bash scripting -- popd with directories containing spaces

Share uncovered secrets, useful notes, guides and hacks!

bash scripting -- popd with directories containing spaces

Postby unrequited » Tue Jan 08, 2008 3:59 pm

So I'm messing around with my OLPC, finally learning linux because I've put it off so long, and I'm getting into the bash scripting to do things I'd rather not repeat over and over. Here's my question, how do I pass directory strings to push/popd that have spaces in them? Here's a basic script I want to flesh out later to run a command in each directory under a current tree:

Code: Select all
#!/bin/bash

echo "starting craps, now in directory: `pwd`";

tree -idf --noreport |while read k;
* * do
* * * * echo "pushing $k to dirstack";
* * * * pushd $k 1>/dev/null;
* * * * echo "executing b.s. command in `pwd`";
* * * * popd 1>/dev/null;
* * done;

echo "done, now in directory: `pwd`";


This works FINE until you get to a directory with a space in the name...

I've tried piping the tree command to sed to try and escape the spaces:
|sed 's/ /\\ /g'

and even piping all that into sed again to encapsulate it all in single quotes:
|sed 's/.*/'''&'''//g'

but I'm stumped.

From the prompt itself, you can certainly do a:
popd ./directory with spaces
and it will work just fine and if you want, you can scrap the space escaping and go straight to single quotes:
popd './directory with spaces'

but neither will work in a bash shell script passed to popd.

Can anybody help?
-Unrequited
unrequited
 
Posts: 25
Joined: Sat Jan 05, 2008 2:59 am

Postby unrequited » Tue Jan 08, 2008 4:32 pm

Wow head into wall... all I had to do was quote the $k... nm

pushd "$k" 1>/dev/null;
-Unrequited
unrequited
 
Posts: 25
Joined: Sat Jan 05, 2008 2:59 am


Return to Tips, Tricks & Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests