The Stack Overflow podcast is back! Listen to an interview with our new CEO.

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
vote
2answers
47 views

Block all default commands while a bash script are running?

I want to make a custom commands directory, that only works when the shell script are running and when the user stops the script all will fall back to the default without altering the default commands ...
0
votes
0answers
30 views

Return multiple params, some with spaces, from command substitution [duplicate]

I have a JS compiling command that I've boiled down to this (export VAR=hello\ world;~/args.sh --var:$VAR $(for i in $(seq 1 3); do echo "--var:$VAR --var2:$i "; done)) where ~/args.sh is a simple ...
2
votes
1answer
61 views

any way to make this command chain shorter or better

I am using this command chain to filter out bot/crawler traffic and ban the ip addresses. Is there any way i can make this command chain more efficient? sudo awk -F' - |\\"' '{print $1, $7}' access....
0
votes
1answer
33 views

Why does `tr` not work with find and with command substitution? [duplicate]

I want to transform/"translate" all files in a folder from lower to uppercase. I could use a for loop to do that: for i in ./* ; do mv $i $(echo $i | tr "a-z" "A-Z") ; done for i in `find . -maxdepth ...
0
votes
2answers
79 views

Why less won't be displayed during the execution of a command substitution?

I have an interactive script that returns a path for cd to use in the parent process. Like this: $ cd $(myscript) During the execution of the script, I want to be able to display some information in ...
0
votes
2answers
17 views

cd on the last folder tcsh

trying to cd on the last directory in bash if im trying this : cd $(ls -l | tail -l) it will result in : Illegal variable name. I'm not sure how I should do this
2
votes
2answers
37 views

bash: Match and replace user input with another array

First of all, sorry for the heading. I'm not sure how to place my ambition in a few words. However, from a coding/scripting standpoint it should be pretty basic. Though, I somehow fail to achieve it :/...
0
votes
2answers
50 views

How to add 1st / 2nd argument to bash script being sourced from stdin

I have this: bash <( curl_url='https://raw.githubusercontent.com/oresoftware/run-tsc-if/master/run.sh' curl -H 'Cache-Control: no-cache' --silent "$curl_url" ) the bash script at that url, ...
2
votes
1answer
45 views

Combining xargs, sed and command substitution [duplicate]

I would expect under bash for the following: ls *.py | xargs -I{} echo $(echo {} | sed 's/\.py/_2\.py/g') to list all .py files contained in the directory but with _2 annexed after the file name ...
0
votes
2answers
50 views

Automatically hold directory name from inside a tar, by a variable

I download a tarball containing only one directory; this directory has a very long name. I desire to change that directory's name once I extract it from the tarball but I prefer another way than ...
0
votes
3answers
52 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
56 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")[...
2
votes
4answers
551 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
49 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 ...
0
votes
1answer
31 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
220 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
50 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
33 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
127 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
38 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
343 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
14 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
278 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
48 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
34 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
34 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
62 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
417 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
223 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
78 views

Understanding $() (and for-loops) in bash [duplicate]

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
41 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 -...
11
votes
3answers
640 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
51 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
66 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
1k 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
37 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
88 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
762 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
235 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
891 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" ...
3
votes
5answers
73 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
90 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
301 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
50 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
129 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 ...