Questions tagged [command-substitution]

Questions about shell command substitution (such as $(command) or `command`), its mechanism, correct syntax etc. Not to be confused with aliasing.

Filter by
Sorted by
Tagged with
-1
votes
0answers
20 views

Bad substitution in zip operation [on hold]

I rent a CetnOS "Shared" server environment (I think version 4.2) with Bash 3.2.52(2) with set -x and complete -r. I executed: #!/bin/sh war="$HOME/public_html" # Web Application Root domain="...
0
votes
3answers
34 views

How do I join the results of a command substitution?

I want to create a list of mp3 files like a 1.mp3|a 2.mp3. I have tried echo ${(j:|:)$(echo *.mp3)} and some variations of it, but they don’t work: # too much splitting 01|The|Magic|Finger|01.mp3|...
2
votes
1answer
52 views

How to split an expression by space in bash?

I am getting the valid result I want in zsh but not bash, what am I missing? I am trying to split the string into words. Bash bash-3.2$ echo ${$(echo "Hello World")[@]} bash: ${$(echo "Hello World")[...
1
vote
1answer
1k views

Job Control: How to save output of background job in a variable

Using Bash in OSX. My script has these 2 lines: nfiles=$(rsync -auvh --stats --delete --progress --log-file="$SourceRoot/""CopyLog1.txt" "$SourceTx" "$Dest1Tx" | tee /dev/stderr | awk '/files ...
2
votes
4answers
427 views

How to prevent command substitution on the command line?

I find that when writing text as input to another program, any command substitutions in double quotes within the intended text are interpreted and expanded by the shell The links in the answer here ...
1
vote
1answer
39 views

Makefile commands with external args and internal variables

In a Makefile target I have a list of files that I want to split into x parts and work only with one part y, then pass those file names as argument to a test runner. I have no control over the ...
21
votes
4answers
12k views

What's the difference between `curl | sh` and `sh -c “$(curl)”`?

One easy install method for Docker (for example) is this: curl -sSL https://get.docker.com/ | sh However, I have also seen some that look like this (using the Docker example): sh -c "$(curl -sSL ...
0
votes
1answer
30 views

Repeat commands with changes

I have to repeat this command a lot of times a day, is there any way to execute it multiple times (aprox. 25 times) cd ../folderName && ../../tools/clone.py What I need to do is execute the ...
4
votes
1answer
151 views

Processes in a session in an interactive shell vs in a script

If I run this command in a script it doesn't produce output except header from the ps: # Taken from Advanced Bash Usage on youtube: echo "$(echo "$(echo "$(echo "$(ps wwf -s $$)")")")" This is ...
1
vote
1answer
40 views

Why do I have “command not found” in my script?

I am writing a Nagios plugin and I have a "command not found" when I run it. What is wrong? Why 127.0.0.1 (or localhost as well) is not recognized? When I run just the snmp request - I have a simple ...
1
vote
1answer
27 views

how to execute compound commands in shell script?

Here's my script: #!/bin/sh result=$((diff <(sort 1.txt) <(sort 2.txt))) if [[ $result != "" ]] then echo ERROR else echo PASS fi I got an error when execute this script: chk.sh: ...
1
vote
2answers
79 views

Bash: Escaping double quotes in $() command substitution [duplicate]

I am having trouble wrapping my head around how command substitution works when part of the command's parameters come from a variable. To illustrate, I'll just present a series of commands: I'll ...
1
vote
0answers
33 views

What is difference between `` and $() for command-substitution in bash scripting? [duplicate]

I'm new to Linux and StackExchange. My English is pretty bad, and I'm sorry for mistakes. What is the differences between `` and $()? Does it matter what to use and when? I'll be grateful for ...
1
vote
2answers
33 views

Bash Perl multiline match into variable

I am trying to extract 2 strings from a multiline text. postmsg="\033[0;37m Email Settings" This line finds some match at the beginning of the string and puts it in a variable inCol. inCol=$(perl -...
4
votes
2answers
323 views

NUL delimited variable

GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu) Idea is to set a variable to a NUL delimited data set. Here $samples This, however, result in: warning: command substitution: ignored ...
3
votes
1answer
2k views

Is `echo $TEST` expanding an asterisk in the variable a bug? [duplicate]

Is this a Bash bug? $ mkdir test && cd test && echo "a" > "some.file" test$ echo '*' * test$ TEST=$(echo '*') test$ echo $TEST some.file Why is the second output the resolution of ...
5
votes
5answers
2k views

How to deal with spaces in a variable

I'm working on some scripting: for x in `find ./ -name *.pdf` do echo pathname $x done My filenames are Test1 ( Volume II), Test2 ( Volume II). I'm getting a return of pathname Test1 pathname ( ...
0
votes
1answer
11 views

SMB Not Able to Use Date Command Substitution

smbclient //10.10.101.29/it -W WORKGROUP -U user --password pass -c 'put ./file_$(date +%Y_%m_%d).file ./folder/file_$(date +%Y_%m_%d).file' The above does not work & gives me an error stating: ....
2
votes
1answer
216 views

egrep in bash script not working

I have this set of commands : s3ls="aws s3 ls $ssyss3/ --recursive --human-readable --summarize" egrepCommand="egrep '$currentDate|Total'" totalSize=$(echo $s3ls|$egrepCommand| awk -F 'Total Size:' '{...
0
votes
0answers
26 views

Nesting back-ticked commands [duplicate]

I have this which won't work: source_dir=`cd `dirname `readlink "$BASH_SOURCE"`` && pwd`; what does work is this tho: source_dir=$(cd $(dirname $(readlink "$BASH_SOURCE")) && pwd); ...
1
vote
1answer
47 views

Why does this bash idiom require eval?

Why does this produces _results=""... _results="$( grep ${_gopts[@]} )" And this produces the desired _results (list of SSHFS entries in fstab)... _results="$( eval grep ${_gopts[@]} )" The _gopts ...
112
votes
6answers
89k views

How can I execute `date` inside of a cron tab job?

I want to create a log file for a cron script that has the current hour in the log file name. This is the command I tried to use: 0 * * * * echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log ...
0
votes
1answer
32 views

How to check “eval” command?

My code docker-machine create --driver virtualbox dev VBoxManage list vms "minikube" {9c326ed5-faf4-42fe-acda-bf3a283f1a74} "kalinew" {de6de631-0d51-4638-b967-66db463cbf05} "dev" {84a116bf-02b9-...
2
votes
1answer
32 views

How I can read content of the ssh key file using a path read from a file?

I need to read content of the ssh key using the given path resides in a file like following: my ssh private key is: ~/.ssh/firstname.lastname When I call cat ~/.ssh/firstname.lastname in terminal on ...
3
votes
2answers
2k views

Is there a way to execute code in the command line prompt (PS1) without using backticks?

I am using this piece of code directly in the command prompt (PS1), and it tells me whether the previous command executed correctly. PS1="\`if [ \$? = 0 ]; then echo -e \"\e[1;32m[⚡️ ]\"\e[0m; else ...
5
votes
2answers
4k views

When should one use $( ) in defining variables

Some shell scripts I have come across use the following syntax when defining variables: file_list_1=$(echo "list.txt") or file_list_2=$(find ./) I would have used: file_list_1="list.txt" and ...
5
votes
3answers
519 views

Linux command substitution order

I'm confused with the command substitution. I think command substitution as like a programming language macro. The sub shell is executed first and the $(...) is substituted with it's standard output, ...
0
votes
1answer
250 views

difference between echo ($x) and a=“($x)”; echo $a

When I execute the following in terminal: x=1 a=($x) echo $a #output: 1 echo ($x) #output: -bash: syntax error near unexpected token `$x' Why are above outputs different? Also why are the ...
5
votes
3answers
2k views

Capture the output of a shell function without a subshell

I have rbenv (ruby version manager) installed on machine and it works like that: $ rbenv local 2.3.1 Writing to stdout the local version of my ruby. I want to rescue this version and declare it in a ...
1
vote
5answers
2k views

Run a command on all the files in a directory tree and put the output into a variable

I want to run this bash command : #!/bin/bash rep="*" for f in `ls -R`$rep; do d='git log '$f'| wc -l' c=$d echo $c done how to excute a command git log myFile | wc -l from bash ? ps : this ...
3
votes
3answers
24k views

How do I suppress stderr warning messages from a command inside command substitution?

touch ~/deleted/$(echo "directory_"$(readlink -f foo)|tr '/' '\') 2> /dev/null where foo is a directory It's reading the full pathname of foo and creating it as a file replacing all '/' with '\' ...
0
votes
1answer
60 views

Why value of a is 1?

I'm learning shell scripting and I am writing a script which lists the file for which the user has "7" permission, i.e. "rwx". So far I've written this: for F in * do a= $(stat -c "%a" "$F") ...
6
votes
3answers
7k views

How to preserve the newline character (\n) when capture output of a command in a variable

As a simple example, I have a bunch of source code files. I want to store the "head" command output to a variable for all these files. I tried: output=$(head $file) but what happened is that this ...
0
votes
1answer
174 views

echo, pipe and command substitution

I want to substitute an echo command followed with | (pipe). For example, $(echo "echo 'hello' | cat") returns 'hello' | cat I expect this to behave like echo 'hello' | cat which returns hello ...
0
votes
3answers
258 views

Change directory working directory for previous directory in Shell Script

Is there any way to achieve below.. I am stuck to perform my task. I have written shell script which gives me correct output only when the script is getting executed on same month below is the ...
0
votes
3answers
781 views

Returning local values from Bash variables?

I studied this article called Returning Values from Bash Functions. Data Lorem. \begin{document} hello \end{document} Case #1 which does not work Code #!/bin/bash function getStart { ...
0
votes
1answer
49 views

Understanding $() (and for-loops) in bash

I have a directory with git inside and git branch gives me the following output: $ git branch branch1 * branch2 master The * marks the current branch. I would like to use this output in a script....
5
votes
2answers
8k views

What is command substitution in a shell? [duplicate]

I'm using Ubuntu 16.04 with Bash and I tried to read in Wikipedia, in here and in here, but I failed to understand what is the meaning of "command substitution" in shell-scripting in general, and in ...
1
vote
2answers
37 views

How to use regular expression result into another expression

I have two regular expressions i.e. command1 and command2 where i need to combine both expressions into a single expression using | for that command1 output should be passed to next expression. ...
108
votes
6answers
331k views

Pass the output of previous command to next as an argument

I've a command that outputs data to stdout (command1 -p=aaa -v=bbb -i=4). The output line can have the following value: rate (10%) - name: value - 10Kbps I want to grep that output in order to store ...
0
votes
1answer
30 views

Linux - Bash - variable assignment with command substitution is missing the newlines [duplicate]

I am running into a bit of an issue / quirk here: [root@s06p5bcsmta001 elves]# TEST=$(cat -e sendlog) [root@s06p5bcsmta001 elves]# echo $TEST | wc -l 1 [root@s06p5bcsmta001 elves]# cat sendlog | wc -...
10
votes
4answers
4k views

Understanding Bash's Read-a-File Command Substitution

I am trying to understand how exactly Bash treats the following line: $(< "$FILE") According to the Bash man page, this is equivalent to: $(cat "$FILE") and I can follow the line of reasoning ...
11
votes
2answers
12k views

How do I diff the outputs of two commands? [duplicate]

How can I use the diff command to compare 2 commands' outputs? Does something like this exist? diff ($cat /etc/passwd) ($cut -f2/etc/passwd)
20
votes
1answer
994 views

What does POSIX require for quoted here documents inside command substitution?

In this question someone reports a problem using a here document with a quoted delimiter word inside $(...) command substitution, where a backslash \ at the end of a line inside the document triggers ...
0
votes
2answers
136 views

can not realize what is wrong with: $ echo ‘`date`’

What happens when the following command is executed? Explain why, and how to fix it: $ echo ‘`date`’
10
votes
3answers
623 views

How can I generate arguments to another command via command substitution

Following on from: unexpected behaviour in shell command substitution I have a command which can take a huge list of arguments, some of which can legitimately contain spaces (and probably other ...
2
votes
3answers
2k views

Pass output of “whereis” command to “cd” to change directory in one step

I can't find a way to pass output of whereis command to cd command in same line so I don't have to do cd in the second step. I have tried passing like below: cd $(whereis node_modules) Or cd "`...
5
votes
3answers
358 views

Why doesn't the conditional work when I use it directly without a variable?

This gives me an error that says too many arguments: if [ $( file -b $i ) == "directory" ] But when I tried this name=$( file -b $i ) if [ name == "directory" ] It seems to work just fine. Can ...
6
votes
1answer
26k views

Return code for curl used in a command substitution

I've script as below :- (edited) httpUrl="http://www.nnin.com" rep=$(curl -v -X POST -d "UID=username&PWD=pass" $httpUrl) status=$? if [ "$?" -eq 0 ]; then echo "Success" exit $status ...
2
votes
2answers
399 views

Difference between piping and command expansion

This link is relevant What's the difference between substitution and piping to bash but I am not quite understanding everything that is being said. What is the difference between piping command1 ...