Reputation: 707
I want to put the output of command in bash variable and then further use that variable in other command
Suppose i want something like this
ls | $(variable) |awk '/$variable/{print "here"}'
Upvotes: 2
Views: 12513
Reputation: 10224
You can try:
variable=$(ls); awk "/$variable/"'{print "here"}'
Note 1: /$variable/
is surrounded by double quotes, otherwise it won't be replaced by output of command.
Note 2: The above command may fail since the output of ls
may contains "/" or newline, which will break the awk
command. You may change ls
to something like ls | tr '\n' ' ' | tr -d '/' | sed 's/ *$//g'
(replace all newlines with spaces; delete all slashes; remove the trailing whitespace), depending on your goal.
Note 3: to avoid variable assignment polluting the current shell's environment, you can wrap the above command by parentheses, i.e. (variable=$(some_command); awk "/$variable/"'{print "here"}')
Upvotes: 0
Reputation: 31
Or
now=`date`
Back ticks
Which is easier for me since it works in any shell or perl
Upvotes: 0
Reputation: 5338
To put command output into variable you can use following format in bash
variable=`pwd`
echo $variable
Upvotes: 2
Reputation: 224844
I don't know that you can easily do it in a single step like that, but I don't know why you'd pipe it to awk
and use it in the script like that anyway. Here's the two step version, but I'm not really sure what it does:
variable=$(ls)
echo ${variable} | awk "/${variable}/{printf \"here\"}"
Upvotes: 0