Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

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
32 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 -...
3
votes
2answers
291 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 ...
0
votes
1answer
7 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
171 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 ...
0
votes
1answer
30 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
23 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 ...
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") ...
0
votes
1answer
174 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 ...
0
votes
1answer
123 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
1answer
43 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....
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. ...
0
votes
1answer
25 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
3answers
614 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 ...
1
vote
1answer
40 views

Shell: Is it possible to give a string as a file to a command?

Suppose we have a command, eval-some-language —-path=<filename> And we want to do something like this: eval-some-language —-path=“say hello world! exit 3” I think the following solution ...
0
votes
1answer
34 views

Find in command substitution: Empty result inside while, result in command line

I have this bash script to find files recovered by PhotoRec (files I know are there, but because of their number I can't locate them one by one), but the find command doesn't return any results, if I ...
0
votes
1answer
44 views

How can I use command expansion on `\{\}` in `find -exec`?

I would like to find some directories via find /path/to/a/dir -type d -links 2 and then for each pathname found by find, assumed to be stored in variable pathname, I would like to stow -d "$(...
0
votes
1answer
732 views

[: =: unary operator expected [duplicate]

I need to write a script to test if a service inside of an Android emulator is ready for apk installation or not using this command adb -s emulator-5554 shell pm list package | grep package:com....
0
votes
1answer
32 views

Why are redirections not working following assignment?

./NulFile contains NUL. When command expansion removes NUL from it, it outputs warnings. Is there some way to suppress the warnings? Why are redirections not working? $ filecontent="$(cat ./NulFile)"...
1
vote
1answer
73 views

How can I prevent command substitution from removing NUL and trailing newline(s)?

An Stackoverflow answer says that command substitution removes NUL and trailing newline(s), and provides the following commands to prevent it from doing so, FILE="$(mktemp)" printf "a\0\n" > "$...
1
vote
2answers
435 views

Why is eval not working with command substitution? [closed]

I have this command $("${DIR}/test.py" "$1") the output of which is export SOME_VAR="hello world" export ANOTHER_VAR="hello" But eval $("${DIR}/test.py" "$1") seems to do nothing. What am I ...
4
votes
1answer
196 views

cut command fields

I noticed these two different fields behaviors using cut command: bash:~$ var=`cat /proc/cpuinfo | grep 'model name' | uniq | cut -d ' ' -f 3,4,5,6,7,8 ` echo $var outputs Intel(R) Core(TM) i7-...
7
votes
1answer
778 views

exit not terminating the script [duplicate]

exit doesn't terminate the script when error is called.. output Error: Could not resolve localhost after exit script #!/bin/sh resolve_ip (){ if [ -z "$1" ]; then host="localhost" ...
1
vote
2answers
34 views

Pass length of argument into bash command substitution

I'm trying to write a quick bash function that populates a README.md with a $1\n followed by underscores the length of $1. The code I found in other stackexchange questions showed that to print a ...
0
votes
1answer
43 views

Use command output as argument in watch command

I have a watch on jq -r '.' $(ls -t | head -1)" The goal is to apply "jq -r '.'" to the most recently modified file in directory. Unfortunately, the command used to know the last modified file is ...
3
votes
2answers
292 views

Bash parameter substituiton within commands

I have a passing understanding of Parameter Substitution, including substrings like so foo="Hello World"; greeting=${foo:0:6} But do I do this with commands? greeting="Hello" md5greeting=$(echo $...
0
votes
1answer
32 views

Use derived output from a series of commands to egrep from pstree -p command

I have the following long one liner which essentially provides a list of PIDs with a '|' delim character echo $(lsof -p $(pgrep -f dosemu | tr '\012' ,) | grep '/media/datadrv' | awk '{print $2" - " $...
1
vote
1answer
81 views

Weird behavior in zsh random generator

I have this script to generate $1 digit(s) of mixed random char from the defined charlists as arrays. #!/bin/bash charlist1=(a b c d e f g h i j k l m n o p q r s t u v w x y z) charlist2=(0 1 2 3 4 ...
0
votes
1answer
40 views

How to get bash not to treat [ as a test character when using command substitution? [duplicate]

How can I get unmodified output from $() in bash? I need to have bash return the results of command substitution (via $(command) or `command`), but bash treats the left square bracket as the test ...
0
votes
3answers
125 views

Is command substitution syntax, $(), considered to be an operator or a command?

I can't find any official documentation that refers to it as one or the other. What, exactly, is the technical distinction between a command and an operator, anyway?
2
votes
2answers
234 views

How to disable word splitting during command substitution?

How can I disable word-splitting during command substitution? Here's a simplified example of the problem: 4:00PM /Users/paymahn/Downloads ❯❯❯ cat test.txt hello\nworld 4:00PM /Users/paymahn/...
3
votes
2answers
1k views

Checking results of a command in Bash script

I am reading "Advanced Bash-Scripting Guide" at the moment. There is some script that not working correctly on my machine: HNAME=news-15.net # Notorious spammer. # HNAME=$HOST # Debug: test for ...
0
votes
1answer
22 views

How to write a shell script that takes a name of a folder and deletes all subfolders?

I'm stuck with the script which I wrote on Bourne Shell. Script: echo "Who are you?" read Individual echo "Hello,$Individual" echo "Where you want to go?" read Path grep -c "Path" file.txt
0
votes
0answers
18 views

Command substitution, assignment, tests and spaces [duplicate]

I know the whole language is confusing, but I am having trouble understanding assignments with command substitutions: x=Example sentence #1 fails due to space x='Example sentence' ...
2
votes
2answers
130 views

Shell Command Substitution Difference/Anomaly

When scripting in Bash, we learned to assign a variable to the output of a command, we can use the construct: variable=$(command-string) This is known as command substitution and executes the ...
2
votes
5answers
166 views

What problem/pattern justifies using command substitution?

I raised a few server environments with Ubuntu and Bash, on "self managed shell" hosting providers like DigitialOcean, on which I ran Drupal/WordPress applications. All that time I didn't have a ...
9
votes
1answer
639 views

When printing a variable that contains newlines, why is the last newline stripped? [duplicate]

Contents of file.txt (no weirdness, text file as defined by POSIX) iguana gecko anole Sample script: #!/bin/sh string="$(cat file.txt)" printf '%s' "$string" Sample output: [coolguy@somemachine ...
15
votes
1answer
1k views

What's the right way to quote $(command $arg)?

It's high time to solve this conundrum that's been bothering me for years... I've been meeting this from time to time and thought this is the way to go: $(comm "$(arg)") And thought my view was ...
0
votes
1answer
17 views

Unable to resolve $proc_date from the script

I have multiple HQL's, below is the one example. located at : /home/ganesh/CopyJobs/hql/ insert into XYZ.exttbl_form_data PARTITION (load_date="$proc_date") select FORM_DATA_ID,FORM_ID,USER_ID,...
3
votes
1answer
290 views

Why is shell treating a part of the output of $(<file) as a command?

I saw this line while reading a blog on IFS that is : for i in $(<test.txt) And thought that $(<test.txt) prints the file contents to STDOUT. I maybe wrong in this, but out of curiosity I ...
4
votes
2answers
5k 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 ...
2
votes
2answers
360 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 ...
-1
votes
1answer
472 views

tail and bash: different output when same is expected

I'm trying to sort my logs and found that single digit dates were cut incorrectly. Now I want to either add a 0, or remove the extra space. Prior to that, I wanted to see how to get the correct lines ...
1
vote
4answers
189 views

How to make bash substitution $(<“filename”) silent

I want to replace cat: var=$(cat "filename" 2>/dev/null) by bashism syntax: var=$(<"filename") The problem is that I don't know how to make the bashism silent to avoid such warnings: bash: ...
5
votes
2answers
1k views

Bad substitution: no closing “`” in a heredoc / EOF

I'd like to print a man style usage message to describe a shell function like this output man find: NAME find - search for files in a directory hierarchy SYNOPSIS find [-H] [-L] [-P] [-...
0
votes
3answers
211 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
1answer
1k views

Concalc - “tcsetattr error: Inappropriate ioctl for device”

In a bash script I use the following: #!/bin/bash calc=$(concalc "$*") printf "%s\n" "$calc" I do get the result I want, but allways with this error message: tcsetattr error: Inappropriate ioctl ...