This question already has answers here:
How can I have a newline in a string in sh?
(13 answers)
Closed 3 years ago.
I want to create a variable that contains a string of text [in BASH] that spans multiple lines and includes spaces. I can do this without spaces, but not with spaces.
I am trying to create a flash card program for self study. I am not in school and this is not homework.
This works:
$ TEST=me$'\n'you
$ echo "$TEST"
me
you
This does not work:
$ TEST="me on line one$'\n'you on line two"
$ echo "$TEST"
me on line one$'\n'you on line two
I would like the output of the second code to look like this:
me on line one
you on line two
Note: this is not a duplicate question. The alleged duplicate does not contain an example with spaces, which is why I had a problem.
$ test=$'me on line one\nyou on line two'
$ echo "$test"
me on line one
you on line two
If you insist on using different quoting methods for different parts of the string (why?), you need something like
$ test="me on line one"$'\n'"you on line two"
Related
This question already has answers here:
Echo newline in Bash prints literal \n
(22 answers)
Closed 1 year ago.
A very simple question. I'd like to write several lines to a file with just one line containing multiple \n. I.e. my input string would be hello\nhello\nhello, I'd like to write it to a file hello.txt, and when cat hello.txt it should give me
hello
hello
hello
However, when I do echo "hello\nhello\nhello" > hello.txt, it just gave me the literal string, without converting \n to a newline.
I wonder how to write such a line to a file.
You want either echo -e or, generally even better printf
printf 'Hello %s\n\nWelcome to my %s document\nmultiline document\n\n\n\nEnd\n' \
"John" "yellow" > "/some/path/hello.txt";
This question already has answers here:
Capturing multiple line output into a Bash variable
(7 answers)
Closed 5 years ago.
I have a line in my shell script like below
output_logs=`sh script2.sh $1 $2`
script2.sh produce lots of lines of logs. I want to grep specific lines of its output. The problem I have is $output_logs has the entire output of script2.sh as a single line string and grep produces strange results because of this. How to get the logs from script2.sh to be stored in individual lines?
Maybe you should try something like this.
output_logs=`sh script2.sh $1 $2`
echo "$output_logs"
Note that this is different from echo $output_logs.
The double-quoted version of the variable preserves internal spacing of the value exactly as it is represented in the variable — newlines, tabs, multiple blanks and all — whereas the unquoted version replaces each sequence of one or more blanks, tabs and newlines with a single space.
This question already has answers here:
Losing newline after assigning grep result to a shell variable
(4 answers)
Closed 7 years ago.
I have a program that is trying to print multiple lines from a text document. I have my main bash program, and I am using the line:
Multlines=`</Users/$USER/Documents/text.txt`
echo $Multlines
Where text.txt may look something like
John
Smith
but the echo prints John Smith where I want it to print it on different lines.
How do I print it out that way?
To preserve the newline characters in the variable when printing with echo, you need to double quote it:
echo "$Multlines"
You could also just use cat instead of echo, assuming you had it available to you.
This question already has answers here:
How to substitute quoted, multi-word strings as arguments?
(4 answers)
Closed 7 years ago.
script.sh:
#!/bin/bash
echo "First argument: $1"
wrapper.sh:
#!/bin/bash
CALLER='./script.sh "this should be one argument"'
$CALLER
what happens:
$ ./wrapper.sh
First argument: "this
what I was expecting:
$ ./wrapper.sh
First argument: this should be one argument
I tried different exercises to make it work the way I want it, but I can't find the way to invoke script.sh with single argument containing spaces from within wrapper.sh.
I would also like to understand the way nested quotes are interpreted.
This works instead (only last line changed):
#!/bin/bash
CALLER='./script.sh "this should be one argument"'
eval "$CALLER"
The reason for this is that quoting is applied at a different place in the parsing process than variable substitution, so you need to re-run the result of the substitution ($CALLER) through the parsing process (using eval), but quoted (the "…" around $CALLER) to avoid the field splitting that comes with the substitution already.
Further reading: the POSIX documentation on this, and the links already given in comments.
This question already has answers here:
Bash and filenames with spaces
(6 answers)
Closed 8 years ago.
I'm writing a script to do variable substitution into a Java properties file, of the format name=value. I have a source file, source.env like this:
TEST_ENV_1=test environment variable one
TEST_ENV_2=http://test.environment.com/one
#this is a comment with an equal sign=blah
TEST_ENV_3=/var/log/test/env/2.log
My script will replace every occurence of TEST_ENV_1 in the file dest.env with "test environment variable one", and so on.
I'm trying to process a line at a time, and having problems because looping on output from a command like sed or grep tokenizes on white space rather than the entire line:
$ for i in `sed '/^ *#/d;s/#.*//' source.env`; do
echo $i
done
TEST_ENV_1=test
environment
variable
one
TEST_ENV_2=http://test.environment.com/one
TEST_ENV_3=/var/log/test/env/2.log
How do I treat them as lines? What I want to be able to do is split each line apart on the "=" sign and make a sed script with a bunch of substitution regex's based on the source.env file.
sed '/^ *#/d;s/#.*//' source.env | while read LINE; do
echo "$LINE"
done
An alternative is to change $IFS as per #Jim's answer. It's better to avoid backticks in this case as they'll cause the entire file to be read in at once, whereas piping the output of sed to while above will allow the file to be processed line by line without reading the whole thing in to memory.