herestring

bash 3 and bash 5 evaluate herestring whitespace differently

牧云@^-^@ 提交于 2021-01-29 17:31:13
问题 I have a script: #!/bin/bash { read a read b } <<< $(echo a; echo b) declare -p a b I wrote it to f , did chmod +x ./f , and expected that bash ./f and ./f would be identical. They aren't: 💻~/dev/test[1]$ ./f declare -- a="a b" declare -- b="" 💻~/dev/test[2]$ bash ./f declare -- a="a" declare -- b="b" I figured out that bash ./f is using /usr/local/bin/bash which is version 5.0.16, and that ./f using /bin/bash is version 3.2.57. What changed between those versions to make this evaluate

sh - split string by delimiter

若如初见. 提交于 2021-01-27 23:36:36
问题 code s='id;some text here with possible ; inside' IFS=';' read -r id string <<< "$s" echo "$id" error restore.sh: 2: restore.sh: Syntax error: redirection unexpected bash version GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu) 回答1: You seem to be using sh to execute the script. Herestrings aren't supported in sh ; hence the error. Ensure that you're using bash to execute the script. 回答2: A here string is just a shortcut for a small here document. This should work in any POSIX shell:

Do here-strings undergo word-splitting?

不羁的心 提交于 2020-07-18 08:50:05
问题 Quoting Bash Reference Manual and man bash (version 4.3): [ n ]<<< word The word undergoes brace expansion, tilde expansion, parameter and variable expansion, command substitution, arithmetic expansion, and quote removal. Pathname expansion and word splitting are not performed. The word should not undergo word-splitting. However, using this simple code: var="a b" cat <<< $var #output: #a b cat <<< "$var" #output: #a b What am I missing? Does this depend on the version of bash or is there a

Here string adds line break

旧时模样 提交于 2019-11-30 03:02:53
问题 It seems that here string is adding line break. Is there a convenient way of removing it? $ string='test' $ echo -n $string | md5sum 098f6bcd4621d373cade4e832627b4f6 - $ echo $string | md5sum d8e8fca2dc0f896fd7cb4cb0031ba249 - $ md5sum <<<"$string" d8e8fca2dc0f896fd7cb4cb0031ba249 - 回答1: Yes, you are right: <<< adds a trailing new line. You can see it with: $ cat - <<< "hello" | od -c 0000000 h e l l o \n 0000006 Let's compare this with the other approaches: $ echo "hello" | od -c 0000000 h e