Questions tagged [here-string]

A special, more compact form of the here-document consisting of <<< followed by a single WORD.

39 questions
16
votes
3 answers

Cannot create temp file for here-document: Permission denied

[Note: This similar Q concerns the same bash error message. It's been marked a duplicate of this other Q. But because I found a very different source for this error, I will answer my own Q below.] This previously working bash script line while ...…
Elliptical view
  • 3,559
  • 4
  • 25
  • 44
12
votes
2 answers

Why does wc <<<"$string" show a one-byte-longer length than printf "$string" | wc?

Accidentially, I found out that wc counts differently depending on how it gets the input from bash: $ s='hello' $ wc -m <<<"$s" 6 $ wc -c <<<"$s" 6 $ printf '%s' "$s" | wc -m 5 $ printf '%s' "$s" | wc -c 5 Is this - IMHO confusing - behaviour…
rexkogitans
  • 1,319
  • 11
  • 17
11
votes
1 answer

What is the advantage of using bash -c over using a here string?

Is there any real benefit to using bash -c 'some command' over using bash <<< 'some command' They seem to achieve the same effect.
yosefrow
  • 389
  • 3
  • 12
10
votes
4 answers

Why does cut fail with bash and not zsh?

I create a file with tab-delimited fields. echo foo$'\t'bar$'\t'baz$'\n'foo$'\t'bar$'\t'baz > input I have the following script named zsh.sh #!/usr/bin/env zsh while read line; do <<<$line cut -f 2 done < "$1" I test it. $ ./zsh.sh…
Sparhawk
  • 19,561
  • 18
  • 86
  • 152
9
votes
1 answer

What are the differences between here document and here string in their purposes?

In bash, it seems to me both here document and here string can be used for providing strings as stdin inputs. here document may provide an extra features to specify delimiter, which I am not sure if is necessary in any case. What are the differences…
Tim
  • 98,580
  • 191
  • 570
  • 977
7
votes
3 answers

How can I use the bash <<< operator with diff?

Please, observe: $ diff <(echo a) << b I also know <<< works fine in general: $ cat…
mark
  • 387
  • 1
  • 9
6
votes
2 answers

What is more efficient or recommended for reading output of a command into variables in Bash?

If you want to read the single line output of a system command into Bash shell variables, you have at least two options, as in the examples below: IFS=: read user x1 uid gid x2 home shell <<<$(grep :root: /etc/passwd | head -n1) and IFS=: read…
FedKad
  • 560
  • 3
  • 15
6
votes
2 answers

Are Here-Strings available in ksh88?

I wanted to extract 3 words from a variable in 3 different variables in ksh script. I used this line on ksh93 and it worked fine: read A B C <<< $line Got this error for the above command while running it on ksh88: syntax error at line ## : '<'…
Pratik Mayekar
  • 609
  • 1
  • 5
  • 10
5
votes
1 answer

What is really happening in this command?

When I use the command cat <<< Hey > text.txt text.txt I expect it to write "Hey" in the file text.txt and then display the file. But there is no output. How is bash interpreting it actually? Command and its output: $ cat <<< Hey > text.txt…
sh.3.ll
  • 301
  • 2
  • 10
5
votes
1 answer

Elegant way of diff'ing two variables?

I have $a and $b. I want to run diff on those. The best I have come up with is: diff <(cat <<<"$a") <(cat <<<"$b") But I have the district feeling that I am missing a clever Bash syntax to do that (as in "Why don't you just use foo?").
Ole Tange
  • 33,591
  • 31
  • 102
  • 198
4
votes
1 answer

Why does a Bash 'while' read loop with a command substitution herestring not read the entire input?

Look at this very basic Bash script: #!/bin/bash while read l do echo $l echo "next one" done <<< $(ps aux) I have several processes on my computer, and the ps aux command works fine in the terminal. I only get one iteration in the loop and ps…
Bob5421
  • 173
  • 1
  • 7
4
votes
3 answers

Variable not interpreted with 'EOF'

This is my script: var="lalallalal" tee file.tex <<'EOF' text \\ text \\ $var EOF Need to use 'EOF' (with quotes) because I can not use double slash (//) otherwise. However, if I use the quotes, the $var variable is not expanded.
Marcus
  • 49
  • 1
  • 2
3
votes
1 answer

backtick or Here string or read is not working as expected in RHEL 8

I am trying to redirect the output of a python script as an input into an interactive shell script. test.py print('Hello') print('world') Say test.py is as above prints "Hello world" which is feed to two variables using Here string redirection as…
3
votes
3 answers

Redirect content under a new line (without further syntax or arguments)

I tried these ways to append content to a file: printf "\nsource ~/script.sh" >> /etc/bash.bashrc echo -e "\nsource ~/script.sh" >> /etc/bash.bashrc cat >> "/etc/bash.bashrc" <<< "source ~/script.sh" All three ways work. I find the cat herestring…
Arcticooling
  • 1
  • 12
  • 44
  • 103
3
votes
2 answers

How to use mapfile/readarray

I have some code similar to this: while read -r col1 col2 col3 col4 col5 col6 col7 col8 TRASH; do echo -e "${col1}\n${col2}\n${col3}\n${col4}\n${col5}\n${col6}\n" done< <(ll | tail -n+2 | head -2) (I'm not actually using ls / ll but I…
jesse_b
  • 35,934
  • 12
  • 91
  • 140
1
2 3