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
2
votes
2answers
376 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
542 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
194 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
233 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 ...
0
votes
1answer
412 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
251 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
178 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
105 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
45 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
758 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
181 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
36 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
985 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:...
2
votes
2answers
89 views

Mixing local and remote variables and proper usage and quotation

The recommended pattern for bash as far as I know is to always quote the shell variables. E.g. "$VAR" instead of $VAR. But how can I achieve the same safety the quotes provide for variables meant to ...
0
votes
2answers
611 views

Why the remote ls from here doc from ssh does not work within a $()? [duplicate]

I am trying to count the number of files in a remote directory. I have the following code: ssh server.com<<RUNTHIS FILE_COUNT=$(ls -AU /foo/bar/test_dir) echo "$FILE_COUNT" > ~/...
0
votes
1answer
48 views

Why doesn't this command work like I expect? [closed]

cat $(echo this\\ list) Seems like it should be the same as cat this\ list but it isn't. I know that I can use cat "$(echo this\\ list)" but then I can't echo multiple files into cat. Why doesn'...
3
votes
1answer
3k views

bash command substitution with redirection: bad file descriptor

the following command works in dash but fails in bash with "Bad file descriptor". $ dash -c 'out=$(echo "to fd3" >&3; echo "to stdout") 3>&1; echo "out: $out"' to fd3 out: to stdout $ ...
2
votes
2answers
106 views

Preventing shell from exiting when set -e is turned on?

I have set -e turned on for my script. The only thing is there is one command here that I don't want causing the script to exit if it fails, but I want everything else to do that. How can I keep set -...
0
votes
2answers
136 views

shell expanding rename command backreferences

When I'm using group matched pattern in rename command and use their back references in replacement part and I also want to have command substitution expansion as well within my rename command and ...
-1
votes
1answer
623 views

echo with back-tick

In a script, I got the following code from a ksh script : for log_file in `cat a_filename` do `echo mv from_directory/$log_file to_directory` done Could the code be for log_file in `cat ...
1
vote
2answers
1k views

Behaviour of bash command substitution with command from string in variable

$ echo $(echo x; echo y) x y $ a='echo x; echo y' $ echo $($a) # expect 'x y' x; echo y Why does command substitution behave in such way? How to perform the command substitution for list of commands ...
2
votes
1answer
91 views

Why is the IFS value removed from the file while splitting?

IFS=$'?\n' for line in $(cat "./newfiletoread") do printf "${line}\n" done The file contents are as below: Hello there!How are you doing?How is life?Mine is as boring as a winter morning! The code ...
0
votes
2answers
59 views

Escape correctly this variable string so it doens't output

I have this shell script: UNZIP_FOUND=$(dpkg-query -W --showformat='${Status}\n' unzip|grep "install ok installed") if [ "" == "$UNZIP_FOUND" ]; then echo unzip not found, I will install ...
1
vote
2answers
5k views

How can I evaluate the result of an adb shell command?

For example, I want to check if a directory exists on the phone. R=$(adb shell 'ls /mnt/; echo $?' | tail -1); $ echo $R 0 $ if [ "$R" -ne 0 ]; then echo "Path doesn't exist"; else echo "Path ...
0
votes
0answers
10 views

Can't pass parameters to find from function execution [duplicate]

I have the need to pass multiple parameters to find dynamically. I'm doing that using this function: GEN_FIND_PARAMS() { PARAM="" for WORD in $1; do ADD="" if [ X"$PARAM" != ...
5
votes
3answers
505 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, ...
3
votes
2answers
658 views

blocking/non-blocking pipes/redirects inside command substitution

I observed the following behaviour in bash: { echo 'foo' ; sleep 10 ; } -> standard output "foo" appears immediately, after 10 seconds the command is done (as expected) { echo 'foo' ; sleep 10 ; } &...
0
votes
4answers
126 views

ksh loop: “for dir in find .. do” not working, unlike “for dir in ls .. do”

Both of the following lines do return the list of dirs of one of my directories. The first of them use a name pattern whereas I need to choose dirs given they really are directories to then process ...
0
votes
1answer
468 views

Subshell inside quotes

I'm writing a small shell script like this: curl -X POST --header 'Bearer "$(printf user:pass | base64)"' 'https://api.com/v1/auth' To debug I switched to echo: echo 'Bearer "$(printf remote-key-...
1
vote
3answers
3k views

run commands inside awk before print

I wish to list directories while calculating their size and print the whole thing in a simple command, using ls and awk, work great to control the list and what I wish to display : ls -AlhF /usr | ...
1
vote
1answer
1k views

Command substitution in if statement [duplicate]

I'm trying to compare the result of a command substitution to a string, like this: if [$(ping $1)=="ping: unknown host localhosts"] then echo "no"; else echo "yes"; fi What am I doing wrong here?...
1
vote
2answers
497 views

Capture stdout and stderr as separate variables in fish shell

Is this even possible? I'd like to run a command but capture its stdout and stderr as separate variables. Currently I'm using set -l var (cmd), which leaves stderr untouched. I can do set -l var (cmd ...
1
vote
1answer
43 views

Extracting parts of whitespace separated string

I have several Git repositories containing a file mergedriver.info This file looks always like this: <project name> <repository name> A script, triggered by a Git merge driver, is ...
0
votes
3answers
183 views

Confusion about bash command vs. variable substitution

I've been writing one-line bash scripts on the command line since 1989. These usually have the form: for name in 1 2 3; do wc $name.txt; done Now I'm trying to do some image manipulation with GIMP, ...
2
votes
1answer
146 views

Strange output with command subtitution using backticks in bash script

echo "testing:" PROXY_URL="/proxyurl/" proxyUrlSedEscaped=`echo "$PROXY_URL" | sed -e 's/[\/&]/\\&/g'` echo "$PROXY_URL" | sed -e 's/[\/&]/\\&/g' echo "$...
0
votes
1answer
312 views

capturing subshell command substitution output

I'm trying to capture the output of a command substitution. This works until a pipe is added to the command. Am I doing something wrong or is this a quirk of mingw? $ bash --version GNU bash, version ...
4
votes
3answers
350 views

Can it be explained; the difference in array behavior between the use of array=$( command ) and array=( $( command ) )?

I understand command substitution. I understand subshells. I do not understand why using a subshell changes the structure of my array. Given this command output: (use of openstack command is not ...
4
votes
1answer
2k views

Find a file and make a symlink to parent using find and -exec

I am trying to use find to find files matching a certain pattern, and then symlink their parent directorys to another directory, this is my current script (I'm doing this on mac so -printf won't work ...
1
vote
1answer
518 views

Storing output of shell script to variable

I have read many threads on this topic, but none of the solutions have worked for me. I am trying to do the following: RELEASE_COMMAND_OUTPUT=$(exec ~/temp/execs/github-release release --user ...
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 ...
0
votes
1answer
479 views

Alias accepting variables from the result of evaluation [duplicate]

I was trying to define an alias that helps me to cd to the directory that is created most recently, and I'm using the following in my .bashrc: alias cdlatest="latestdir=$(ls -td -- */|head -n 1); cd $...
0
votes
2answers
575 views

Find size of file names with space and hyphen | pass file names containing space and hyphen to “du” [duplicate]

I have a file, which contains the name of some files. I want to find the total size of files in the list. #cat filelist /tmp-directory/connector_db_ connector_db /tmp-directory/connector_db -...
3
votes
1answer
765 views

Filenames with spaces in backtick substitution

Suppose I have a file containing a space in its name such as with space. What should cmd output in the below snippet for ls to accept 'with space' as argument? $ ls `cmd` I tried ls `echo 'with ...
0
votes
1answer
127 views

How to enable newline seperate when run command `ls -l` when stdout is not a terminal

I store the command in a variable cmd, and get the output of the command using this statement: echo `$cmd` everything works fine except ls -l, the newline is missing.
6
votes
2answers
177 views

Do I need to quote command substitutions when assigning their output to a variable? [duplicate]

I tend to quote command substitutions as shown below even when assigning their output to a variable: var="$(command)" Is that actually needed though? When does it break? The accepted answer here ...
0
votes
1answer
89 views

netcat pipe running too early?

I have the following script running in the background: while true; do { file=$(/somescript); echo -ne "HTTP/1.0 200 OK\r\n\r\n"; cat $file; } | nc -l -p 20000 done It works fine, except that /...
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`’