echo variable with content from command substitution
Solution 1
When referencing a variable, it is generally advisable to enclose its name in double quotes. This prevents reinterpretation of all special characters within the quoted string -- except $
, `
(backquote), and \
(escape). Keeping $
as a special character within double quotes permits referencing a quoted variable ("$variable"), that is, replacing the variable with its value.
Use double quotes to prevent word splitting. An argument enclosed in double quotes presents itself as a single word, even if it contains whitespace separators.
e.g.
variable1="a variable containing five words"
COMMAND This is $variable1 # Executes COMMAND with 7 arguments:
# "This" "is" "a" "variable" "containing" "five" "words"
COMMAND "This is $variable1" # Executes COMMAND with 1 argument:
# "This is a variable containing five words"
Enclosing the arguments to an echo statement in double quotes is necessary only when word splitting or preservation of whitespace is an issue.
For more info and examples go here
Solution 2
You have newlines because ls
puts them on separate lines. The newlines disappear without the quotes because the shell (bash) passes each unquoted space separated text to the command as a separate argument.
Note: The command substitution is done by the shell, not by ls
, so you do not need ls
.
Therefore you can do
#!/bin/bash
echo *.fastq
or
#!/bin/bash
files="*.fastq"
echo "$files"
Solution 3
When you refer to a variable without quotes around it (e.g. echo $files
), the shell splits the value apart on whitespace and passes each term as a separate command-line option. Newlines are treated the same as any other whitespace. The echo
program doesn't see the newline characters at all; it just gets an array of strings, each of which is a single filename.
When you refer to a variable in quotes (e.g. echo "$files"
), the shell doesn't do any whitespace splitting; instead, it passes the entire value, unmodified, as a single argument. The echo
program receives one long string that includes the newline characters.
Variables aside, this is the same behavior you get with quotes around literal values. If you write echo foo bar
, the spaces are stripped out by the shell and echo
just gets the strings foo
and bar
, and it'll print those strings with a single space between them, since that's how it's coded to combine multiple arguments. If you write echo "foo bar"
, echo
gets the single string foo bar
and will print it as-is.
Related videos on Youtube
user3138373
Updated on September 18, 2022Comments
-
user3138373 almost 2 years
I wrote a very basic script of command substitution which is below:
#!/bin/bash files=$(ls *.fastq); echo $files
The directory contains bunch of .fastq file and I just want to use echo command to output them
The above sript outputs fastq files with a space between each fastq filename.
When I use it in this way
#!/bin/bash files=$(ls *.fastq); echo "$files"
it prints the results on new lines.
Why is it so? Is it something to do with command substitution?
Thanks
-
user3138373 almost 9 yearsrunning ls *.fastq on the terminal outputs on the same line separated by spaces, how is ls putting them on separate lines then?
-
ctrl-alt-delor almost 9 yearsI can not find the docs, but
ls
will output same asls -x
when to a tty, and likels -1
when not. Therefore usels -x
to get what you want.