Questions tagged [command-substitution]

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

234 questions
3
votes
1 answer

Shell Expansion (Command substitution) in Golang

Go has the support for variable expansion, for example: os.ExpandEnv("test-${USER}")` >> "test-MyName" But is there a way of expanding executables, as the way the shell behaves? Something like os.ExpandExecutable("test-$(date +%H:%M)") >>…
jwong
  • 338
  • 6
  • 15
3
votes
1 answer

How to perform multiple commands on a list of files in bash (xargs?)

I have a file that contains a list of files, and I want to perform two commands on each file. Contents of files.txt: file1 file2 file3 The commands I want to perform on each file are (for example) ls -s and du. I want the output to end up being…
localhost
  • 1,253
  • 4
  • 18
  • 29
3
votes
3 answers

Bash IFS ('\n'): Problems with file name detection through find command and for loop

#!/bin/bash IFS='\n' declare -i count=0 AX=$(find *.iso -maxdepth 1 -type f) # Rather use AX="$(find *.iso -maxdepth 1 -type f"? # A="${AX%x}" < Could I use this when applying "" to $() in AX? But it should already include newlines like this way.…
3
votes
1 answer

Script logic from within bash command substitution

I have the following snippet in a script: exec 3<<<"$(zenity --entry --title="Enter PIN" | validate_pin || error_handler )" Elsewhere, I must pass the PIN through a file descriptor, hence the redirection and command substitution here. The output of…
Hyppy
  • 146
  • 5
3
votes
1 answer

How can I differentiate "command substitution" from "subshell" inside a script?

I need to differentiate two cases: ( …subshell… ) vs $( …command substitution… ) I already have the following function which differentiates between being run in either a command substitution or a subshell and being run directly in the…
Maxim Kholyavkin
  • 4,463
  • 2
  • 37
  • 82
3
votes
2 answers

How to make zsh fail the whole line when a command substitution fails?

If I do: $(blah) bleh I get zsh: command not found: blah zsh: command not found: bleh This has me a little worried, because it means that zsh is executing bleh which was never intended to be a command; what if it happens to be executable and does…
Owen
  • 38,836
  • 14
  • 95
  • 125
3
votes
2 answers

How does Bash's IFS variable affect command substitution?

I am writing a bash script that loops over the output of a command substitution and then tries to perform another command substitution within the loop body. Here is the code: #!/usr/bin/bash IFS=$'\n' for i in $( xmllint --xpath…
user2141130
  • 954
  • 7
  • 22
3
votes
2 answers

Bash command substitution stdout+stderr redirect

Good day. I have a series of commands that I wanted to execute via a function so that I could get the exit code and perform console output accordingly. With that being said, I have two issues here: 1) I can't seem to direct stderr to /dev/null. 2)…
Taelo
  • 55
  • 1
  • 4
3
votes
2 answers

Shell programming: nested expressions with command substitution

I have trouble handling expressions within other expressions. For example, here's my code: #!/bin/sh number=0 read number if [ `expr substr $number 1 2` = "0x" ]; then echo "Yes that's hex: $number" number=`expr substr $number 3 `expr length…
Linas
  • 560
  • 1
  • 5
  • 16
2
votes
3 answers

Using find(1) in command substitution and quote filenames with blanks

I'd like to use find inside a command substitution, where the returned filenames contain whitespace. What option do I need so it correctly quotes the filenames? I tried -print0, but it will not work in the shell itself. example: command $(find .…
knittl
  • 246,190
  • 53
  • 318
  • 364
2
votes
1 answer

Tar list of files resulting from command

I have a folder with 200k pdf files and want to tar them. Which of the following solutions would be better? I suspect the command substitution might run into problems with the huge number of files because of command line length limitations. Process…
Roland
  • 7,525
  • 13
  • 61
  • 124
2
votes
4 answers

Correct way of quoting command substitution

I have simple bash script which only outputs the filenames that are given to the script as positional arguments: #!/usr/bin/env bash for file; do echo "$file" done Say I have files with spaces (say "f 1" and "f 2"). I can call the script with…
pfnuesel
  • 14,093
  • 14
  • 58
  • 71
2
votes
0 answers

"find and xargs" don't work as expected: How to get the basename of an arg from xargs?

I want to shrink some pdfs in a folder using a script and cron. The main command is as follows: find . -maxdepth 1 -name "*.pdf" -print0 | xargs -0 -i shrinkpdf.sh -out="./folder/file.pdf" in={} I want the outfile to have the same name as the…
rakurtz
  • 21
  • 1
2
votes
1 answer

Pass conditional arguments to curl in bash

I have a bash script calling curl, and I want to pass a certain argument only if some condition is fulfilled: result=$( curl -sS --retry 3 --retry-delay 10 \ # some more stuff $([[ $b=="b" ]] && echo -F \"foo=bar\")…
Linus
  • 1,574
  • 2
  • 15
  • 23
2
votes
1 answer

'command not found' when passing variable into BASH function, am I quoting incorrectly?

So I have a command that basically adds a line to a file, but only if that line isn't already in the file. It uses grep to check the file and if not there then appends to the file. The purpose of this is because I want to import all my aliases into…
AdeGoodyer
  • 23
  • 5
1 2
3
15 16