0

How can I combine these two examples with pushd and whoami to change the directory?

I know I can change the directory like this:

#!/bin/bash
pushd /home/mike/Pictures > /dev/null
# do something in the new dir
ls
popd > /dev/null

And I know I can get the username like this:

#!/bin/bash
theuser=`whoami`
echo $theuser        
2
  • 1
    Uh... change it to what? Commented Dec 31, 2011 at 19:24
  • You don't really need whoami, if I understand your request correctly. Just pushd -n ~/Pictures would do. Commented Dec 31, 2011 at 19:27

6 Answers 6

3

You're waaay overthinking it...

cd ~/Pictures

EDIT:

Actually, no. What you really want is:

cd "$(xdg-user-dir PICTURES)"
Sign up to request clarification or add additional context in comments.

2 Comments

Ignacio, Do you know if xdg-user-dir comes in Ubuntu natty by default? I mean in a new install, after applying security updates but before applying any recommend updates? Chrisaycock, I read the man for pushd but I still don't understand the -n option. What if I wanted to create a new directory after changing directories? I would need to omit the -n correct?
@mike: I'm not certain, but just in case you could put the result into a variable and use parameter substitution to replace ~/Pictures if it's empty.
2

Those backticks can be used to interpolate the output of the command they contain into another:

pushd /home/`whoami`/Pictures

4 Comments

Use $(...) in preference to back-quotes. It is especially valuable if you need to nest the construct (which is not the case here).
Thanks rkb, I took a break then came back and used your option. I still need the >/dev/null and popd > /dev/null correct?
Redirecting stdout to /dev/null will hide the normal output of those commands... so if that's what you want, then you still need them =) And popd to get back to the directory you were in before. Lots of good answers here, depending on what you really need to do with your script.
perfect comment, the backticks are really useful in general - thank you for that info, didn't knew that before.
0

Much easier than using pushd and popd, run the command in a sub-shell:

(
cd /home/$(whoami)/Pictures &&
ls
)

The sub-shell changes directory, without affecting the main process - exactly as you wanted, but rather more reliably.

Comments

0

It's easier just to use cd to change the directory:

#!/bin/bash    
cd ~/Pictures

Comments

0

Among other solutions:

pushd "$HOME/Pictures"

After all, nothing obliges the home directory to bear the user name!

Comments

-1

Bash already has the $USER variable, no need to call an external binary

pushd /home/$USER/Pictures > /dev/null

2 Comments

If I go with this do I still need the popd > /dev/null ? I guess I don't fully understand what it does.
If you're running this as a regular script (i.e. it's executable, starts with "#!", and you run it with the filename NOT the source or . commands), it'll run in a subshell. This means that when it does a cd, the subshell cd's to that directory, but your shell remains right where it was; when the subshell exits, it doesn't matter where it had cd'd to. Note that @Jonathan Leffler's answer can be used to force a subshell, in case this is not running as a regular script, or needs to do something back in the original directory after the ls.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.