9

I know that pwd gives the current working directory, hostname gives the current host and whoami gives the current user. Is there a single unix command that will give me the output of

whoami@hostname:pwd

so that I can quickly paste the output into an scp command?

1
  • 2
    echo $(whoami)@$(hostname):$(pwd)? Why do you need this? The user for SSH/SCP defaults to the current user, the working dir defaults to the home directory if omitted. At least you need scp file.txt host: (where host can be a DNS name or alias from ~/.ssh/config)
    – Lekensteyn
    Nov 20, 2013 at 15:52

2 Answers 2

8

Not a single command as far as I know, but this does what you need:

echo "$(whoami)@$(hostname):$PWD"

You could make that into an alias by adding this line to your shell's rc file (~/.bashrc, or ~/.zshrc or whatever you use):

alias foo='echo "$(whoami)@$(hostname):$PWD"'
6
  • That works, thank you. Can you explain why the command PWD is in caps but hostname and whoami are not? I'm not sure I understand the distinction and I think it might be important.
    – Hooked
    Nov 20, 2013 at 15:55
  • PWD is environment varible, which is called by $PWD. There are plenty of other environment variables (like $PS1, the first part of your command prompt). The others are using $() syntax to use the output of a command in place. This is a basic overview, at the very least, of what the difference is. When you starting getting further into the command line, and start making aliases and functions and bash scripts, it's something you'll learn to love. I wish I could think of some of the crazy things I've done with this!
    – Rob
    Nov 20, 2013 at 15:59
  • @Rob Thanks, I understand now, but why use $PWD over $(pwd), shouldn't the output always be the same? Is one better than the other or is this just personal preference?
    – Hooked
    Nov 20, 2013 at 16:01
  • @Hooked well, honestly I doubt there is much difference but pwd runs one more command and I (think) that will be more expensive than querying the variable. In practical terms there will be no real difference though, use whichever you prefer.
    – terdon
    Nov 20, 2013 at 16:07
  • 1
    $(whoami) runs the command whoami and uses the output, and $PWD just grabs an environment variable. Forking off fewer processes is better for speed and performance, but in this case it doesn't really make a difference. If you do printenv it should list all the environment variables. I have a $USER, so I could use echo "$USER@$(hostname):$PWD"
    – Rob
    Nov 20, 2013 at 16:16
1

This does the job as well (with the use of environment variables):

echo "$USER@$HOSTNAME:$PWD"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .