Questions tagged [command-substitution]

Command substitution is the replacement of a command with the result returned after it is evaluated.

234 questions
1
vote
2 answers

How does Bash assign the result of a command substitution to a variable when stdout of that command contains spaces?

Lets say I have the following two Bash scripts: # script1 var=$(./script2) echo "$var" # script2 echo "x y" Running ./script1 outputs x y. As noted here, "Bash performs the expansion by executing command in a subshell environment and replacing…
Moritz Wolff
  • 436
  • 1
  • 7
  • 16
1
vote
1 answer

What does "line 10: acuteness: command not found" mean and what is wrong with my if statement?

Here's my script: #!/bin/bash #read password.lst, hash each word, store in variable and check it against our hash target_hash="14a4b8e7ae966e72a3a2c51633bfabc6" password_lst=/usr/share/metasploit-framework/data/wordlists/password.lst while IFS=…
1
vote
1 answer

Bash - Command substituting $(ping google.com) outputs to terminal

I've been writing a script whose weird behavior's been driving me nuts, but I've managed to find what might be the problem: command substituting like this out="$(ping google.com)" if done while the internet isn't available, outputs this to the…
Michele
  • 25
  • 6
1
vote
3 answers

uuidgen and $RANDOM doesn't change in find -exec argument

I want to get all the instances of a file in my macosx file system and copy them in a single folder of an external hard disk. I wrote a simple line of code in terminal but when I execute it, there is only a file in the target folder that is replaced…
1
vote
1 answer

Pass contents of file as a string to an argument for another command

I have a json file like this: {"my_string": "apple", "my_int": 456} And a Bash program that works OK like this: my_program --message='{"my_string": "apple", "my_int": 456}' But when my JSON is a file I cannot seem to get it to run…
CClarke
  • 503
  • 7
  • 18
1
vote
1 answer

Capturing output to variable only works with redirection

I know that the construct to capture stdout to a variable is var=$(...). However why does the following not work? docker pull nginx version=$(docker run --rm --entrypoint nginx nginx:latest -version) echo $version I found, however, adding 2>&1 to…
minh ly
  • 143
  • 2
  • 12
1
vote
1 answer

Variable in command substitution is expanded into multiple arguments

I have tried to create a method in a bash script which should be able to perform a curl operation with a variable number of headers, however I seem to get stuck in that the curl command consider the header arguments to be multiple arguments as they…
M. A. Tanaka
  • 167
  • 1
  • 2
  • 13
1
vote
1 answer

Bash: command substitution when reading from a file

Not sure if I am being dumb here but I am not able to figure this out. If I put this in bash script: var="`date '+%a %b %d'`*.*INFO Ended execution of script" echo $var The output that I get is: Mon Sep 27*.*INFO Ended execution of script However,…
1
vote
1 answer

AWK: append text to specific line with 'system(external programm)' substitution

I have a file called data that looks like this: orange apple pair mango grape by using awk -i inplace '{if (NR==4) $0=$0" is sweet"}{print}' data, I get: orange apple pair mango is sweet grape I'm trying to achieve the same result through system()…
1
vote
1 answer

lsblk -n -o NAME,SIZE,TYPE /dev/sd* 2>/dev/null works on cmd line, not in script file

I am writing a script to detect and format USB drives for Raspberry Pi. I got the script to work perfectly for sd cards inserted via a USB adapter. It failed when I tried a usb3 HDD. Isolating the failure indicates a problem with wildcard expansion…
1
vote
2 answers

How to preserve nested command substitutions inside a watch command

I'm utilizing a watch command that has multiple commands strung together and they all work except one piece of it. Even when ran by itself it does not have the desired output. Here is the watch command being ran. watch -n 5 echo "$(echo "$(uptime |…
Shak
  • 35
  • 3
1
vote
2 answers

Command substitution for (GNU coreutils) date in gawk gensub

I have a data file with lines containing a huge amount (~ 5K) of dates in format yy-dd-mm. A tipical file line could be: bla bla 21-04-26 blabla blabla 18-01-28 bla bla bla bla 19-01-12 blabla I need to do this kind of replacement for any single…
vaeVictis
  • 484
  • 1
  • 3
  • 13
1
vote
1 answer

Bash unable to assign value

Got a bit of a problem. I'm new to bash and I'm trying my hardest, but I can't figure out how to assign the desired output to the variable. Running this command in the prompt wc -l data.txt | awk '{ print $1 }' yields the result 12, which is…
Wahlmat
  • 53
  • 3
1
vote
0 answers

Bash - Same command substitution output, but different when echo'ed

This is confounding. I need an explanation of the factor at work here which I'm apparently missing. echo \\z yields: \z echo \\\z also yields: \z Ok fine (presumably because of the way backslashes in command line escaping are discarded even if the…
1
vote
1 answer

Quote handling in variables inside a command substitution

I want to assemble a curl invocation with two variables (one for common arguments and one for the URL) and capture the output via command substitution. This breaks when I add parameters that need enclosing quotes, like say -H "Accept:…
zb226
  • 9,586
  • 6
  • 49
  • 79