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

5
votes
3answers
139 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
34 views

How to escape an input string in Bash [on hold]

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
22 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
65 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
28 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
72 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
52 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 ...
1
vote
1answer
45 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 ...
5
votes
3answers
190 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
75 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
76 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: $ ...
3
votes
2answers
116 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
100 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
139 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: ...
1
vote
2answers
46 views

Apply a filter to the value of a variable using backticks

Sorry for asking such a simple question, but reading about this issue on the internet has proven fruitless. I'm trying to better understand the grave/backtick operator and I've been doing okay so far ...
1
vote
1answer
56 views

How do I capture output into a variable and still have it goto the screen too?

I can do OUTPUT=$(grunt test) and that puts the output into OUTPUT but it's no longer on the screen. I need to see the output as it comes as well, so I can't just wait and then echo $OUTPUT. How can ...
2
votes
1answer
63 views

Bash - Command substitution adds single quotes

I have a problem with bash behavior while evaluating grave accents. I want to run find with optional -name: find -name "*.log" works fine, but when i want to get the -name part from nested script ...
-3
votes
1answer
385 views

does curl -Ls 'http://pastebin.com/raw.php?i=r2jYDZaw' mean it will not output anything?

Does the s in -Ls mean that nothing should be outputted? If so what will be the output then?
3
votes
1answer
81 views

how to send output of curl request to 2 separate commands

I'm piping the output of a curl request to gawk so that I can pull some data. The gawk code is already working if I'm searching through the html file which curl request produces. However, I was ...
2
votes
3answers
64 views

Command substitutions vs backslash escapes in a quoted string

Consider a double-quoted command substitution with backslash escapes inside it, like this: echo "$(echo '\\')" It prints out \\, whereas I would have expected it to print out only one backslash. My ...
0
votes
0answers
15 views

using man command for a system call close [duplicate]

So if we have an executable program called close on linux, and theres also a system call close. How can i generate a command that makes sure i get the manual for the system call "close" rather than ...
4
votes
3answers
53 views

Can command substitution be nested in variable substitution?

I would like to use variable substitution on a particular string that I access via a command. For example, if I copy something into my clipboard, I can access it like this. $ xclip -o -selection ...
2
votes
1answer
251 views

if command in find -exec

I was just trying to list all directories and files under current directory and also write if they are file or directory with the following command: find -exec echo `echo {} : ;if [ -f {} ]; then ...
0
votes
1answer
81 views

Bash escaping is confusing me

Lets say that I have a file (executable) called literally 'a\b'. It just echoes some stuff. Now I want to call that and I can do it by inputting user@machine:~/test$ ./a\\b into my console. Now ...
2
votes
1answer
320 views

Bash: command substitution with multiple unset/export commands

I am trying to unset some variables and export others using command substitution. This works: $(echo " export TEST_A=1 export TEST_B=2 ") And this too: $(echo " unset TEST_A ...
0
votes
2answers
38 views

Command substitution and spaces

Why does this work as a calculator: [root@qabun02 ~]# echo $(( 5+2 )) 7 but this doesn't? [root@qabun02 ~]# echo $( (5+2) ) -bash: 5+2: command not found The only difference I can see is the ...
14
votes
3answers
836 views

Any reason to use or teach `…` substition for new development?

It is my understanding that the more modern $(...) command substitution syntax is preferred over the old `-based syntax, due to easier and less error-prone nesting and escaping syntax. Further, it ...
2
votes
5answers
1k views

grep files from list

I am trying to run grep against a list of a few hundred files: $ head -n 3 <(cat files.txt) admin.php ajax/accept.php ajax/add_note.php However, even though I am grepping for a string that I ...
1
vote
1answer
527 views

Remote ssh command not sending echo variable from a ksh script on AIX

I'm need to get the value of a variable from a remote host in a script using SSH and have to source an environment file first that does a shift at the end of its run. The command works locally and I ...
4
votes
1answer
134 views

cmd2 `cmd1` vs cmd1 | xargs cmd2

I'm trying to find a file with find and then pass the found path to sqlite3. I'm only dropped into the sqlite> prompt if my command is of the form: sqlite3 `find . -type f -iname "*.db" | head ...
1
vote
2answers
274 views

Store stdout in variable and send command to background

I have a command in a bash script which I want to capture the output of and then send it to the background. How do I get this done? The following doesn't seem to work (it keeps blocking and outputs ...
3
votes
4answers
252 views

what is the difference between `find .` and /home/user/* as an input to for command

What is the difference between find . and /home/user/* as an input to for command.? For example: for var in $(find .) do echo "$var" done or for var in /home/user/* do echo "$var" done In the ...
3
votes
1answer
31 views

I need to create file with name is the result of the command in Solaris

I need to create only one file with name is the result of the command ls -l | head -2. I can't use environment variables. The filename must contain newline symbols. I've tried to use touch 'ls -l | ...
3
votes
5answers
1k views

Why do newline characters get lost when using command substitution?

I have a text file named links.txt which looks like this link1 link2 link3 I want to loop through this file line by line and perform an operation on every line. I know I can do this using while ...
0
votes
0answers
78 views

Why is a here-string command substitution considered as a single line?

I'm experimenting with while read syntax and there is a thing I don't understand. The most basic scenario which reproduces the problem is the following: a bash script a.sh calls another script b.sh ...
1
vote
2answers
80 views

How to perform command substitution before brace expansion

I would like to perform command substitution before brace expansion, but couldn't: $ ls {$(seq -s , 13 20)}.pdf ls: cannot access {13,14,15,16,17,18,19,20}.pdf: No such file or directory How can I? ...
0
votes
2answers
221 views

Bash Mysql Command Substitution Login Failure

Bash and MySQL versions: GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu) mysql Ver 14.14 Distrib 5.5.37, for debian-linux-gnu (x86_64) using readline 6.2 I have a bash script that tries ...
2
votes
2answers
59 views

Is there an improved version of “pick” that will work with filenames with spaces?

Is there a version of the pick script that will work properly with spaces in the filenames? This is a tricky question—pick is trivial to enhance to work with spaces in filenames, but the output ...
2
votes
2answers
104 views

Reference a path name in `find` output results

What I would like to do is to be able to reference a find path name result as a parameter to another command without laboriously typing the full path name. Example: $ find -name testfile path1 ...
3
votes
1answer
105 views

How do I diff the outputs of two commands?

How can I use the diff command to compare 2 commands' outputs? Does something like this exist? diff ($cat /etc/passwd) ($cut -f2/etc/passwd)
2
votes
1answer
204 views

Why doesn't PIPESTATUS work on pipelines within a command substitution

Proof below: Bash 4 Prompt> $(echo hello|sed 's/h/m/'|xargs -I{} ls {} 2>/dev/null|sed 's/ /_/') Prompt> for i in ${PIPESTATUS[@]}; do echo $i;done Output> 0 Prompt> echo hello|sed ...
1
vote
2answers
135 views

Why asterisk in command result in for loop is expanding?

I want to write a script that will run git diff --name-status master..<BRANCH> but when I run this: for i in $(git branch | grep -v master); do echo $i; done I get echo one directory ...
1
vote
1answer
52 views

S-Lang shell (slsh): capture command output in a variable

In slsh I know that I have to !-prefix a command to execute it: !ls How can I capture its output in a variable?
3
votes
2answers
190 views

Command substitution declaration syntax in bash - which of these two is better practice? [duplicate]

This is part of a bash find loop, and I wondered which is more correct syntax and why? filename="$(echo "$i" | cut -c5-)"; filename=`echo "$i" | cut -c5-`; Both function for the purpose of getting ...
5
votes
2answers
147 views

“command | less” vs “less <(command)”

A common "idiom" for viewing large amounts of command output is to pipe it to less, via command | less. However, it's also possible (perhaps only in bash, haven't tested in other shells) to use the ...
3
votes
2answers
278 views

How can I list the info for an array of /dev/disks using bash expansion or substitution?

I want the output of: diskutil list; diskutil info [multiple devices] Without having to do: diskutil info disk0; diskutil info disk0s1; diskutil info disk1 ...etc For example, with many builtin ...
1
vote
1answer
42 views

Does the command execute automatically when setting it to a variable?

I'm getting familiar with all about linux & shell/bash scrpiting. I'm configuring a backup script & it will compress an entire dir. I wanna know if I have to print the variable where the ...
4
votes
3answers
431 views

How do I read the output of commands in a script

I'm learning Linux scripting at the moment, and one thing I can get working is assigning a command output to a variable. The command service httpd configtest returns Syntax is OK, so I wrote this. ...
5
votes
2answers
152 views

Command substitution vs I/O Redirection?

I am wondered about differences between I/O Redirection and Command substitution? For example when I want to redirect which command output to cd command (I know that it's not a directory) then the ...
0
votes
1answer
311 views

Commands inside bash not being interpreted?

I'm trying to execute a command inside a bash script, $(pwd) is not being interpreted at all, and not even using eval. This do not work: cat apache-vhost.conf | sed 's/{path}/$(pwd)/g' Neither ...