6

I need to assign the output of a command to a variable. The command I tried is:

grep UUID fstab | awk '/ext4/ {print $1}' | awk '{print substr($0,6)}'

I try this code to assign a variable:

UUID=$(grep UUID fstab | awk '/ext4/ {print $1}' | awk '{print substr($0,6)}')

However, it gives a syntax error. In addition I want it to work in a bash script.

The error is:

./upload.sh: line 12: syntax error near unexpected token ENE=$( grep UUID fstab | awk '/ext4/ {print $1}' | awk '{print substr($0,6)}'
 )'

./upload.sh: line 12:   ENE=$( grep UUID fstab | awk '/ext4/ {print $1}' | awk '{print substr($0,6)}'
 )'
3
  • 4
    What is the exact error, and are you sure you are using bash? Looks fine to me. Commented Jul 17, 2012 at 17:42
  • 5
    This is (presumably) not related to whatever error you're seeing, but -- your whole pipeline can be written as the single command awk '/UUID/ && /ext4/ { print substr($1, 6) }' fstab. Commented Jul 17, 2012 at 17:47
  • 1
    You have an extra single quote at the end of what you copy-pasted as your error message (which differs from what you included above). Commented Jul 17, 2012 at 17:56

2 Answers 2

15

well, using the '$()' subshell operator is a common way to get the output of a bash command. As it spans a subshell it is not that efficient.

I tried :

UUID=$(grep UUID /etc/fstab|awk '/ext4/ {print $1}'|awk '{print substr($0,6)}')
echo $UUID # writes e577b87e-2fec-893b-c237-6a14aeb5b390

it works perfectly :)

EDIT:

Of course you can shorten your command :

# First step : Only one awk
UUID=$(grep UUID /etc/fstab|awk '/ext4/ {print substr($1,6)}')

Once more time :

# Second step : awk has a powerful regular expression engine ^^
UUID=$(cat /etc/fstab|awk '/UUID.*ext4/ {print substr($1,6)}')

You can also use awk with a file argument ::

# Third step : awk use fstab directlty
UUID=$(awk '/UUID.*ext4/ {print substr($1,6)}' /etc/fstab)
Sign up to request clarification or add additional context in comments.

4 Comments

Ok, here is the issue: It works at terminal. However, I want it to work in a script.
@ErayTuncer This is certainly due to a typo in your script.
There shouldn't be a need to pipe awk through awk. The grep is likely not needed either.
@jordanm : Yes of course, my point was to affect the OP command to a shell variable. I will add a shorter command if I have 5 seconds ^^
1

Just for trouble-shooting purposes, and something else to try to see if you can get this to work, you could also try to use "backticks", e.g,

cur_dir=`pwd`

would save the output of the pwd command in your variable cur_dir, though using $() approach is generally preferable.

To quote from a pages given to me on http://unix.stackexchange.com:

The second form `COMMAND` (using backticks) is more or less obsolete for Bash, since it has some trouble with nesting ("inner" backticks need to be escaped) and escaping characters. Use $(COMMAND), it's also POSIX!

6 Comments

Is there actually a difference between `` and $()?
@Shahbaz There is, the $() will work better with nested constructs, while backticks will be more tricky. There was a discussion about this on unix.stackexchange.com, but I can't find it right now. I found it, I'll put it in my answer.
it's definitely more clear to nest using $(), but what I meant was is there an actual difference for bash? Do they behave differently? Or are they different syntaxes for the same thing?
Ok I just saw your update. Dang, I liked backticks, they are so easy to type.
@Shahbaz I usually use the backticks, they work for me, but I am mindful that if I do something more complex I may want to consider the alternatives.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.