Questions tagged [command-substitution]

Command substitution is the replacement of a command with the result returned after it is evaluated.

234 questions
8
votes
3 answers

How to use `set -e` inside a bash command substitution?

I have a simple shell script with the following preamble: #!/usr/bin/env bash set -eu set -o pipefail I also have the following function: foo() { printf "Foo working... " echo "Failed!" false # point of interest #1 true # point of…
krispet krispet
  • 1,648
  • 1
  • 14
  • 25
8
votes
1 answer

Multi-line, double quoted string triggers history expansion on subsequent single-quoted commands it gets piped to

I am on GNU bash, version 4.3.11. Say I want to print unique lines on a file. I am using this approach, which works well on a file: $ cat a 9 10 9 11 $ awk '!seen[$0]++' a 9 10 11 However, if I get the input from stdin, using double quotes in a…
fedorqui
  • 275,237
  • 103
  • 548
  • 598
7
votes
2 answers

Capturing and testing output command in ZSH

I have tried countless ways to get what I want, but nothing seems to work. I always end up with something like 2:not found. I want to capture the output of a command, and then test if it equals "!", like so: function test() { local…
user1218986
7
votes
1 answer

How to escape backticks in bash

I am trying to escape backtick in bash for long time. I tried escaping with \ but it does not work. Is it possible to escape backtick in bash? Sample code I="hello.pdf" var1=`cat <
BhishanPoudel
  • 15,974
  • 21
  • 108
  • 169
6
votes
5 answers

How to address error "bash: !d': event not found" in Bash command substitution

I am attempting to parse the output of a VNC server startup event and have run into a problem in parsing using sed in a command substitution. Specifically, the remote VNC server is started in a manner such as the…
d3pd
  • 7,935
  • 24
  • 76
  • 127
6
votes
3 answers

Escaping backslash in AWK in command substituion

I am trying to escape backslash in AWK. This is a sample of what I am trying to do. Say, I have a variable $echo $a hi The following works $echo $a | awk '{printf("\\\"%s\"",$1)'} \"hi" But, when I am trying to save the output of the same command…
Bill
  • 5,263
  • 6
  • 35
  • 50
5
votes
2 answers

Can I use the $( ... ) syntax with multiline commands?

I often use backticks in with long commands that I break up with backslash-newlines for readability. When I replace the backticks with the $( ... ) syntax, I get errors. For instance, I set a variable using curl and jq inside backquotes (reading…
August
  • 343
  • 3
  • 10
5
votes
1 answer

Why does nesting quotes in backticks work in bash?

More precisely, why does "`command "$variable"`" treat the outer quotes as enclosing the inner quotes, instead of expanding the variable outside any quotes? The exact command I used to test this is similar to an example brought up in another…
Larry
  • 427
  • 2
  • 10
5
votes
2 answers

set environment variable in GDB from output of command

I am trying to exploit a buffer overflow in a challenge, the buffer gets it's value from an environment variable. In GDB I know that you can set environment variables using the command: set environment username = test However I need to pass the…
5
votes
1 answer

Bash: Checking for exit status of multi-pipe command chain

I have a problem checking whether a certain command in a multi-pipe command chain did throw an error. Usually this is not hard to check but neither set -o pipefail nor checking ${PIPESTATUS[@]} works in my case. The setup is like this: cmd="$snmpcmd…
user3040975
  • 615
  • 1
  • 6
  • 10
4
votes
1 answer

POSIX shell: escaping line-continuations in backquote command-substitutions

I'm writing a shell, and I'm a little confused by the POSIX shell specification. Say I have the command: echo "`echo "a\\ b"`" Should the shell output ab or a\ b ? In other words, are line continuations removed again after removing the escaping…
programmerjake
  • 1,794
  • 11
  • 15
3
votes
1 answer

bash - commands in variables with pipes

Can someone explain why A and B behave differently? A=`echo hello how are you | wc -w` and CMD="echo hello how are you | wc -w" B=`$CMD` They give different results: $echo $A 4 $echo $B hello how are you | wc -w What I would like to have is a…
Martin Massera
  • 1,718
  • 1
  • 21
  • 47
3
votes
1 answer

Bash scripting: Encoding string to base64 into a variable

I have a username and password variables. I want to encode them to base64 with the format of username:password. #!/bin/bash username=username password=password echo Encoding username/password... echo $username:$password | base64 That works, but…
Gilbert Williams
  • 970
  • 2
  • 10
  • 24
3
votes
2 answers

Trap with external call in command substitution breaks the parent Bash shell

I have a text-based user interface script that allows me to browse directories and select a file. The graphics are output to stderr, the chosen file's path is sent to stdout. This allows to get the chosen file this way: file="$(./script)" This is…
Informancien
  • 255
  • 3
  • 13
3
votes
0 answers

inlining command substitution in bash parameter expansion

How can I inline a command substitution in a bash parameter expansion instead of having to save the command substitution as a variable and then using that variable in the parameter expansion? # inlined fails in bash; this works, however, in zsh echo…
XDR
  • 4,070
  • 3
  • 30
  • 54
1
2
3
15 16