7

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)}'
 )'
Jon
  • 16,212
  • 8
  • 50
  • 62
Eray Tuncer
  • 707
  • 2
  • 11
  • 31
  • 4
    What is the exact error, and are you sure you are using `bash`? Looks fine to me. – chepner Jul 17 '12 at 17:42
  • 4
    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`. – ruakh Jul 17 '12 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). – sshannin Jul 17 '12 at 17:56
  • `awk '/UUID/ && /ext4/ {print substr($1,6)}' fstab` – tripleee Jul 17 '12 at 19:16
  • possible duplicate of [How to set a BASH variable equal to the output from a command?](http://stackoverflow.com/questions/4651437/how-to-set-a-bash-variable-equal-to-the-output-from-a-command) – Jahid Jun 01 '15 at 18:30

2 Answers2

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)
neuro
  • 14,948
  • 3
  • 36
  • 59
2

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!

ruakh
  • 175,680
  • 26
  • 273
  • 307
Levon
  • 138,105
  • 33
  • 200
  • 191
  • Is there actually a difference between `\`\`` and `$()`? – Shahbaz Jul 17 '12 at 17:48
  • @Shahbaz There is, the `$()` will work better with nested constructs, while backticks will be more tricky. There was a discussion about this on http://unix.stackexchange.com, but I can't find it right now. I found it, I'll put it in my answer. – Levon Jul 17 '12 at 17:50
  • 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? – Shahbaz Jul 17 '12 at 17:53
  • Ok I just saw your update. Dang, I liked backticks, they are so easy to type. – Shahbaz Jul 17 '12 at 17:54
  • @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. – Levon Jul 17 '12 at 17:54
  • The main difference between `$()` and backticks is that, unfortunately, there are still some shells in use that do not recognize `$()` syntax. (even though it has been standardized for over 20 years now!) There is no difference in bash. – William Pursell Jul 17 '12 at 17:54