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

learn more… | top users | synonyms

1
vote
4answers
41 views

Bash file - find value from command output and keep as variable

I am trying to make a script that will conntect to a ups and get the battery value and store it in a variable. I have managed to connect to the ups and get infomation from it but I am strugling to ...
1
vote
1answer
30 views

Substitute with `…` multiline output of find command [duplicate]

I need to substitute output of find command to another command to process every found file, e.g.: mdls `find ~/target_dir/ -iname '*some*' -depth 1` (mdls is a command in OS X which get metadata ...
0
votes
2answers
50 views

Is a script running inside command substitution?

In a POSIX-compatible shell, is it possible to determine if a script is running inside command substitution? my-outer $(my-inner) $(my-other-inner) For the above example, my-inner and ...
3
votes
2answers
37 views

How is the return status of a variable assignment determined?

I have seen constructs in scripts such as this: if somevar="$(somecommand 2>/dev/null)"; then ... fi Is this documented somewhere? How is the return status of a variable determined and how does ...
0
votes
1answer
40 views

capture exit code and output of a command

There are similar questions, but mine is a little bit different. Or so I think. What I'd like to do is: 1.sh: #!/usr/bin/env bash set -eu r=0 a=$(./2.sh || r=$?) echo "$a" echo "$r" 2.sh: ...
1
vote
1answer
63 views

How to escape a range of wildcards in unix shell script?

I need to execute the following Oracle SQL in unix crontab. The query is as follows: select count(*) from tbaadm.htd where cust_id is not null and pstd_flg = 'Y' and del_flg = 'N' and tran_date = ...
2
votes
3answers
68 views

Processing a single file as both input and output throughout pipes [duplicate]

Good evening, I would like to filter a file's content with some piped commands and then write the result back to the same file. I know, I can't do that the way I wrote it. Hold on … This is the ...
1
vote
1answer
43 views

Bash: Default variable with command substitution

Is there any way to accomplish the following in one stroke using Bash? my_var=${$(grep -E '^setting_1' /settings.conf):-default_setting1} As opposed to: my_var=$(grep -E '^setting_1' ...
1
vote
2answers
33 views

How do I create a rule for custom flag replacement with C shell?

I want to have the following behavior with grep: grep --java <rest of the command> Where --java would either dynamically convert to --include=*.java or internally be equivalent to it. Is ...
0
votes
1answer
37 views

How do I execute command to avoid interference from the shell [duplicate]

Sometimes a same command is provided by shell-builtin as well as other file/package. Example: $ type -a printf kill printf is a shell builtin printf is /usr/bin/printf kill is a shell builtin kill is ...
2
votes
1answer
39 views

Error exit script from within command substitution

If I have a function: myfunction() { [ -d somedirectory ] && [ "some other condition" ] || { echo "error" >&2 ; exit 1; } global_var=somevalue } And I call it from within another ...
1
vote
2answers
78 views

Backtick command encapsulation

test@debian:~$ echo `echo \`echo "uh!"\`` uh! How does the Bash do that? It seems that it first executes the expression in the non-escaped backticks, which gives back (the double-quotes "" are ...
1
vote
1answer
52 views

Bash : ambiguous redirect

I am trying to collect dump of java process with jstack and I have tried below command. But its saying ambiguous redirect as I cant redirect to a command. jstack -l `pgrep java` > ...
3
votes
2answers
28 views

vi — replace chars from part of line

I've got a file with all Belgian cities. Some of these cities have boroughs. Lier;stad;Antwerpen;49,70;33930;Lier;Koningshooikt ...
0
votes
0answers
47 views

How to write into local file and set its content to a variable within a ssh command substitution

I would like to write on local file (or local variable if better) and set its content to a variable within a ssh command substitution. Here is my code : REMOTE_OUTPUT=$(ssh remote@server /bin/bash ...
1
vote
2answers
64 views

How to preserve the carriage return 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 ...
-1
votes
2answers
93 views

Nested echo command in backticks

I want to know why an extra echo will print in my shell. I'm using bash version 4.2.46(1). echo `echo `echo $SHELL`` A interesting issue is if I replace '``' with $() it doesn't print extra echo: ...
0
votes
1answer
48 views

Thunar custom action: Extraction to subdirectories

I am attempting to create a Custom Action in Thunar (File Manager) that will extract a gzip archive into a subdirectory of the same name (e.g. abc.tar.gz to abc/). I created this command, which works, ...
1
vote
1answer
30 views

Command substitution

I would like to create a script on SUSE Enterprise Linux, it first asks for a string (representing a date) and then search for all files in a certain directory which contains the string in its ...
2
votes
2answers
83 views

For input redirection, are there functional differences between bash/zsh <<<“$(<file)” and the traditional <file?

I'm asking only about the usage which would have the similar effect as traditional input redirection from a file. <<<"$(<file)" as far as I can tell is equivalent to <file It ...
1
vote
1answer
43 views

Having trouble with img2pdf and spaces in filenames

I am trying to convert a bunch of jpeg files to a pdf file. When I type the filenames with the quotes all is fine: $ img2pdf "./Page 001.jpg" "./Page 002.jpg" -o book.pdf When I go for the ...
5
votes
3answers
305 views

How to prevent bash command substitution output from being escaped?

I am trying to use command substitution in a bash script to output redirection symbols based on a variable like so: IS_VERBOSE=false curl $BLAH $( $IS_VERBOSE && echo '-i' || echo '> ...
1
vote
2answers
53 views

xargs repl_str doesn't expand second placeholder

I would like to rename some files and exclude others into a directory. I tried find . -mindepth 1 -maxdepth 1 ! -name 000-default.conf ! -name default-ssl.conf -print0 | xargs -0 -I {} sudo mv -- {} ...
14
votes
6answers
2k views

Why there is such a difference in execution time of echo and cat?

Answering this question caused me to ask another question: I thought the following scripts do the same thing and the second one should be much faster, because the first one uses cat that needs to open ...
2
votes
1answer
116 views

In bash, grep for a line beginning with \, and assign the result using backticks

In bash, I'm trying to grep a file for a line beginning with a \, and return the result using backticks. For example: echo \\Hello > myFile out=`cat myFile | grep '^\\Hello'` echo $out returns ...
3
votes
2answers
40 views

Command substitution as target for I/O redirection

I was trying to have the output of a command as a target of redirection like this: echo .envrc >> $(git config --global core.excludesfile) But I get this error instead, where ...
2
votes
4answers
39 views

slicing different words from output

Suppose, the result of a command in linux shell is as follows: X and Y are friends Is there any way to slice each of the words (X, and, Y, are, friends) or the first n words from the result, so ...
2
votes
2answers
52 views

why my first pipe operation didn't remove newline character?

My nextermCensus.lis contains newline character and I am only interested in the first line, I'd like to remove newline character. but it doesn't work. so I have to use second method to remove it. ...
0
votes
2answers
70 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 { ...
2
votes
3answers
3k views

echo and command substitution

I wrote a very basic script of command substitution which is below: #!/bin/bash files=$(ls *.fastq); echo $files The directory contains bunch of .fastq file and I just want to use echo command to ...
0
votes
1answer
65 views

bash: cut command produces nothing (just a blank line)

I have a problem with being able to echo a part of a string Here is a simplified version: t=no_1187985:/home/my.name/testing/no I want to echo no_1187985 only so: tt=$(echo $t | cut -d":" -f 1) ...
19
votes
1answer
541 views

Why won't function return until background process ends?

Consider this script: #!/bin/bash function start { leafpad & echo $! } PID=$(start) echo "PID is $PID" The script does not continue past its closing brace until the leafpad process ends, ...
5
votes
1answer
3k views

Bash script error: integer expression expected

I have a problem with a bash script on raspberry pi: x='gpio -g read 22' if [ $x -ge 1 ] then gpio -g write 23 1 fi The error is integer expression expected. Why?
3
votes
2answers
199 views

Which shells don't support dollar-parenthesis expansion, and demand backticks?

The modern recommendation is to use: $(...) instead of the older: `...` mainly for readability and to avoid the need for escaping when nesting. Which shells do not support the $(...) form, and ...
0
votes
1answer
61 views

Calculate swap values dynamically

Can someone please let me know if the following commands should work as I am not sure and am getting error? Just to let you know that I am using this in one of my nagios scripts: ## GET SWAP Warning ...
3
votes
1answer
106 views

Store specific output to a shell variable

I am looking to store the output of the svn info command, more precisely the revision number into a variable for further processing. I just need the 4 stored. Can anyone help me please? $ svn info ...
5
votes
3answers
187 views

Output Output of command to command line directly, NOT variable [duplicate]

I am on the bash shell and I want the output of a command to appear directly in the command prompt that appears after the command has executed ! Example of what I envision it, to illustrate my idea: ...
0
votes
1answer
254 views

How to escape an input string in Bash [closed]

I'm using the jq tool to handle some JSON in bash. Just one problem, while using this line: PB_ACL="acl="`echo $IMGREQ | jq -r'.data.acl'` Result: echo $PB_ACL // acl= The expected value jq ...
0
votes
2answers
32 views

Saving a piped value went wrong

I installed jq to handle some JSON with bash. But somehow piping the JSON string to jq in combination with saving it's output to another variable went wrong. TEST='{"foo": "bar"}' PB_SIG=$TEST | jq ...
2
votes
2answers
189 views

Difference of using () and $() to execute a series of commands

I am currently trying to make a script that creates bytes that will be piped as input to netcat. Here is the idea of the script: (perl -e "print \"$BYTES\x00\"; cat file; perl -e "print \"More ...
0
votes
1answer
728 views

Combine curl and base64 into one command?

I'd like to POST the Base64 encoding of a file to a server, combining curl and base64 into one command that I can execute. I've tried the following: curl 'http://localhost:5000/files' -X POST -H ...
2
votes
2answers
1k views

Zsh: export: not valid in this context

When running this script, I run into an error on this line (relevant snippet below): ... _NEW_PATH=$("$_THIS_DIR/conda" ..activate "$@") if (( $? == 0 )); then export PATH=$_NEW_PATH # If ...
3
votes
2answers
121 views

Simultaneously check for empty output and successful exit status

I'd like to write the following test in an installer script1: if [ -n "`/etc/grub.d/30_os-prober`" ]; then install_dual_boot else install_linux_only fi However, it's also possible that ...
2
votes
1answer
341 views

Nested quotes in subshells

Say I have to use quotes to encapsulate subshell output like: DATA="$(cat file.hex | xxd -r)" But I need to nest this kind of stuff like: DATA="$(cat file.hex | xxd -r | tr -d \"$(cat ...
8
votes
3answers
7k views

Assigning exit code to a shell local variable

#!/bin/bash function0() { local t1=$(exit 1) echo $t1 } function0 echo prints empty value. I expected: 1 Why doesn't t1 variable get assigned the exit command's return value - 1?
5
votes
2answers
194 views

Elegant way to prevent command substitution from removing trailing newline

I'm customizing my zsh PROMPT and calling a function that may or may not echo a string based on the state of an environment variable: function my_info { [[ -n "$ENV_VAR"]] && echo "Some ...
1
vote
5answers
984 views

Output to variable with an indirect command (eval)

What is wrong with this indirect command when run with eval ? #!/bin/bash OS=AIX host=myhost CMD_AIX="(o=\`host \"$host\" \`)" CMD=\$CMD_$OS echo $CMD eval echo $CMD eval "$CMD" Ouput: $ ...
4
votes
2answers
138 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 ...
2
votes
1answer
3k 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 ...
7
votes
1answer
182 views

Why there isn't a new line at the end of quoting a subshell and passing the results to echo?

When I do ls | grep png the output of grep is: 2015-05-15-200203_1920x1080_scrot.png 2015-05-16-025536_1920x1080_scrot.png (filename,newline,filename,newline) then, echo $(ls | grep png) outputs: ...