Home > bash > Bash: command substitution with multiple unset/export commands

Bash: command substitution with multiple unset/export commands

January 28Hits:1
Advertisement

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     unset TEST_B ") 

However, this fails with Bash 4.3.30(1)-release on both OSX and Debian Jessie:

$(echo "     unset TEST_A     export TEST_B=1 ") # bash: unset: `TEST_B=1': not a valid identifier 

Is this an expected behavior, and how can one unset and export multiple variables using command substitution?

To provide some context: here is the related issue in boot2docker.

Answers

In:

$(echo "
  export TEST_A=1
  export TEST_B=2
")

being $(...), just one expansion is parsed as a simple command.

So that $(...) expansion undergoes the split+glob operator. The output of echo is trimmed of its trailing newline characters and split into several words on IFS characters (newline, space and tab by default):

  • export
  • TEST_A=1
  • export
  • TEST_B=2

And each word undergoes globbing. Here they expand to themselves since they don't contain wildcard characters. So after globbing, we still have the same list of four strings.

The command to run is derived from the first one. Here export and all those strings are passed as arguments to that command. So, here, it's as if you had written:

export TEST_A=1 export TEST_B=2

So you're exporting the TEST_A variable with value 1, the export variable without changing its value and the TEST_B variable with value 2.

In the last one, you're running:

unset TEST_A export TEST_B=1

If you want the output of echo to be treated as shell code to interpret, that's where you want to use eval and not use the split+glob operator (quote your command substitution):

eval "$(echo "
  unset TEST_A
  export TEST_B=1
")"

That one is also a simple command. Two strings: eval and <newline><spaces>unset TEST_A<newline><spaces>export TEST_B=1.

eval evaluates the content of its arguments as shell code. So it runs those two shell command lines: unset TEST_A and export TEST_B=1 (again, two simple commands).

Related Articles

  • Bash: command substitution with multiple unset/export commandsJanuary 28

    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 unset TEST_B ") However, this fails with Bash 4.3.30(1)-relea

  • Why does BASH process substitution not work with some commands?October 25

    On occasion process substitution will not work as expected. Here is an example: Input: gcc <(echo 'int main(){return 0;}') Output: /dev/fd/63: file not recognized: Illegal seek collect2: error: ld returned 1 exit status Input: But it works as expecte

  • Command substitution declaration syntax in bash - which of these two is better practice? August 1

    This question already has an answer here: What's the difference between $(stuff) and `stuff`? 5 answers This is part of a bash find loop, and I wondered which is more correct syntax and why? filename="$(echo "$i" | cut -c5-)"; filename

  • Bash - Command substitution adds single quotesApril 11

    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 find `echo "-name \"*.log\""`

  • Bash quotes unescaped on command substitutionMay 4

    Can someone explain me why this is working (listing the contents of a directory that has a whitespace in its name): ret="$(ls "my dir")" Shouldn't it rather be interpreted as: ret="$(ls " my dir ")" As for example w

  • Where has the trailing newline char gone from my command substitution?July 31

    The following code best describes the situation. Why is the last line not outputting the trailing newline char? Each line's output is shown in the comment. I'm using GNU bash, version 4.1.5 echo -n $'a\nb\n' | xxd -p # 610a620a x=$'a\nb\n' ; echo -n

  • Where does the "export" command come from?December 8

    Why doesn't the whereis command give any information about the export command? How does the export command work even if the shell path variable is set to null? --------------Solutions------------- whereis doesn't show you a binary for export because

  • Why do newline characters get lost when using command substitution?October 27

    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 loop but since I am learning, I thought to use a for loo

  • Quoting within $(command substitution) in BashMarch 6

    In my Bash environment I use variables containing spaces, and I use these variables within command substitution. Unfortunately I cannot find the answer on SE. What is the correct way to quote my variables? And how should I do it if these are nested?

  • Bash script to run multiple commands in one block " or sub command"October 3

    I need to make bash script that run multiple commands under specific variable ie, if I type in bash appserver (for example) it runs all the below commands but I need to put them in block : appserver [ read -e -p "please enter weblogic username: "

  • command substitution handling in fish vs. other shells (sh, bash, zsh)July 4

    The program mc (Midnight Commander) is not intended to be used for command substitution, but I would like to know why different shells behave differently when this program (and it seems like other curses programs in general) are used as a command sub

  • Having multi lines in bash command substitutionJuly 8

    I have a very long bash command inside a command substitution like following: $( comaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaand ) I want to know if there is a way to break this command into shorter lines for the sake of readability like following: $(

  • Bash nested command substitution not workingJuly 16

    Hi I have a bash script that intend to run via watch ./script.sh It's to debug a cooling problem with my laptop, however the script I wrote isn't working as intended. I'm quite new to bash scripting so I'm sure its something simple. I have a line in

  • Bash tab completion fails inside of command substitutionAugust 27

    I've been using kill $(pidof ) to terminate programs for a while, and since last night, every time I try to use tab completion inside the $() it throws an error, bash: unexpected EOF while looking for matching `)' bash: syntax error: unexpected end o

  • Bash Mysql Command Substitution Login FailureOctober 9

    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 to capture the result of a MySQL query to a variable u

  • How to fix corrupted bash-completion in command substitution?October 25

    it seems my bash-completion got corrupted. Every time I try to use command substitution, for example when I type ls $(ls Deskto<Tab> ..result is.. ls $(ls Desktobash unexpected eof while looking for matching ")". I tried removing my .bashr

  • Understanding Bash's Command SubstitutionMarch 12

    I am trying to understand how exactly Bash treats the following line: $(< $FILE) According to the Bash man page, this is equivalent to: $(cat $FILE) and I can follow the line of reasoning for this second line. Bash performs variable expansion on $FIL

  • How to prevent bash command substitution output from being escaped?October 15

    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 '> /dev/null' ) That is, if verbose, add the -i switch, otherwis

  • Bash tab complete with non-first token in command substitution $(-) throws errorJanuary 1

    Using tab completion on the first command in $(...) cat $(w When I hit tab I get potential completions as usual. $ cat $(w w wc which wicd-client wofm2opl wpa_supplicant However, in non-first positions bash shows a strange error $ cat $(which w shows

  • bash: "export: command not found- -bash: export: `/usr/local/git/bin': not a valid identifier when opening terminalJanuary 16

    I followed this setup for git: https://www.howtoforge.com/how-to-install-the-latest-git-version-on-centos and seemed like my bash file is broken. gives me this error: bash: "export: command not found... -bash: export: `/usr/local/git/bin': not a vali

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.589 s.