Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to pass the output of an awk command as a variable in a bash script, but not having much success so any help is appreciated.

homedirectory=$(awk /stephen/ /etc/passwd | awk -F/home/ '{print $2}' | awk -F/downloads '{print $1}')

echo '$homedirectory'

I want to extract the home directory of user stephen from the /etc/passwd, so the result I want is 0001





mysql:x:105:107:MySQL Server,,,:/var/lib/mysql:/bin/false


share|improve this question
Do you really need 3 separate awk invocations there? What exactly are you trying to do? It sure looks like you are trying to make things far more complicated then it needs to be. – Zoredache Apr 23 '12 at 20:13
You'd be better off explaining what you're trying to actually do. – MikeyB Apr 23 '12 at 20:13
up vote 3 down vote accepted

Don't look at /etc/passwd. echo ~username, in your case echo ~stephen will do what you want.

From your /etc/passwd result would be

$ echo ~stephen
$ basename `echo ~stephen`

Edit: This was before the edit applied proper formatting to your /etc/passwd contents. I didn't catch the homedir was /home/0001/downloads. What you're asking for is something that will not always have a consistent result unless you always expect to have the inspected user's homedir be in /home. There are a million ways to do this, most which will be somewhat wrong, until you provide information on your expected constraints.

In any case, apply your filtering to the output of echo ~username. You could use a variety of utilities after that, like basename, awk, sed, cut, etc.

share|improve this answer
+1, this is a far more appropriate approach and it should work on all POSIX systems, including Macs (which use etc/passwd only in single-user mode). – Skyhawk Apr 23 '12 at 20:30
The result of basename echo ~stephen is downloads. I could use this I suppose echo ~stephen | awk -F/home/ '{print $2}' | awk -F/downloads '{print $1}' but I am still stuck with how to use the output as a variable. Actually that works, thanks – Stephen Apr 23 '12 at 20:35
Actually, using backticks is pretty 1995 at this point. I'd suggest trying the $(/usr/bin/foo argv) syntax first, as it's more readable. – Magellan Apr 23 '12 at 23:56

Scrap what you have and use:

getent passwd stephen | cut -d: -f6

Or even

cd ~stephen && pwd
share|improve this answer
Not every Unix system has the getent command. It would make more sense to use what is universally available. – Skyhawk Apr 23 '12 at 20:34

Your three awks worked (although as others have said, it could have been done a lot better).

The final problem was that echo '$homedirectory' should have been echo "$homedirectory" since the shell looks inside double quotes but not single quotes.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.