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.

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
99 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
45 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
33 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
36 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
20 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
608 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
30 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
34 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
512 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
27 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
60 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
309 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
180 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
655 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
31 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
31 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
287 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
27 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
71 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
124 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
222 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
21 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
122 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
163 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
620 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
287 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
4k 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
324 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
294 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
173 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
163 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
903 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 ...
0
votes
1answer
306 views

pipe one or more PIDs from pgrep into top's -p argument

I saw this nice question: Pass the output of previous command to next as an argument and tried to get what I wanted from it but no luck. I often want to watch my maxima/lisp process and instead of ...
4
votes
1answer
237 views

Why does bash replaces text from command substitution with text thereafter

I was trying to parse some nginx config λ tree sites-enabled/ sites-available/ sites-enabled/ ├── bank.cwrcoding.com.conf ├── calendar.cwrcoding.com.conf ├── cloud.cwrcoding.com.conf ├── cwrcoding....
4
votes
1answer
158 views

escaping a dollar in a command?

I have a very simple shell script that should remove floating docker images, the command for this is: docker rmi $(docker images -f "dangling=true" -q) Now I'm running into an issue which I think is ...
0
votes
1answer
1k views

Getting no output from command substitution?

I've written a few shell scripts, but have never seen this behavior and am at a loss. I've got the following simple script which runs in a bash shell: LOGFILE="/var/log/constructor-events.txt" ...
-1
votes
1answer
90 views

RAM cleanup operation without rebooting

Say I've stored different variables in RAM and did some actions with wired results when expanding variables and referencing files as in this case. What is the closest cleanup for all "artificial" RAM ...
1
vote
1answer
39 views

How to set an alias upon command execution?

I want other developers to have some convenient methods. Therefore I want to give them a script, that automatically sets an alias. E.g the command grunt compileModule:[modulename] should set the ...
0
votes
1answer
674 views

GNU find: get absolute and relative path in -exec

I have a command (not echo!) that I want to run which takes an absolute path and a relative path. How do I get these two arguments? Attempt: d=/tmp/foo; find "$d" -type f -exec bash -c 'echo d=${1:$...
3
votes
1answer
171 views

Pipelined Sed does not work on found filename inside Bash command substitution when invoked from Find “-exec”

It looks like 'find', 'bash' and 'sed' in some cases does not work as one expects. The following example should first create file 'sample.txt', then find the file and finally process it by '-exec' ...
0
votes
0answers
35 views

Cut in sed issue, unknown option to s [duplicate]

echo $HOME $var /home/user abc My variables. echo $HOME$var | sed "s/$(echo $HOME$var | cut -c 3)/ello/g" /hellome/userabc This works as expected. echo $HOME$var | sed "s/$(echo $HOME$var | cut -c ...
0
votes
2answers
864 views

Makefile, sqare brackets built-in, variable expansion and command substitution

To embrace the DRY (Don’t Repeat Yourself) principle, I sometimes need to share pieces of shell commands in a Makefile. So there is a recipe somewhere in that file like: shell=/bin/bash # … .ONESHELL:...