i need more explanation about echo -n "" > file.txt [duplicate] - bash

This question already has answers here:
What is the difference between "echo" and "echo -n"?
(2 answers)
Closed 6 years ago.
I need to know the meaning of echo -n and this is example: echo -n "" > file.txt. What is the difference between echo and echo -n ?
I read man echo but need more info.

From here:
-n : Do not output a trailing newline.
The > means write it to a file named file.txt (create if doesn't exist).

Related

Bash script echo seems to remove my new lines? [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 5 years ago.
This is the contents of my file file.txt:
header
a
b
c
I have no idea what is going on. This command does not print new lines.
echo -e $(tail -n +2 file.txt)
This prints:
a b c
But if you write it to file you will clearly see new lines:
tail -n +2 file.txt >> new_file.txt
test.txt
a
b
c
How do I force echo to print the new lines? I don't think I can use printf here without making some kind of loop.
Echo statement within quotes give your output with newline terminated lines. Here is the code
echo -e "$(tail -n +2 file.txt)"

How assign rev command to a variable in shell [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 6 years ago.
I wanted to direct the output of rev command to a variable, I tried different methods and didn't work.
read -p "Enter the number: " n
echo $n | rev
echo "new n is: $p"
I want to assign the output of line 2 to p. How?
Thank you,
To store the output of a command in a variable use a $(...) command substitution:
p=$(echo $n | rev)
For further reference, you can check this link

Script: echoing lines from one file to another doesn't print '\t'. Issue [duplicate]

This question already has answers here:
Preserving leading white space while reading>>writing a file line by line in bash
(5 answers)
Closed 6 years ago.
I need to create a file by modifying some lines of a source one.
I developed a loop 'while read line; do'. Inside it, the lines I read and don't modify go just:
echo -e "$line" >> "xxxx.c"
My issue is that some of that lines start with '\t', and they won't print the output file.
Example:
while read line;
do
if echo "$line" | grep -q 'timeval TIMEOUT = {25,0};'
then
echo "$line"
fi
Any help? I've tried with the printf command also but without success.
In that case you could just remove "-e" argument from the echo command.
From echo man page:
-e enable interpretation of backslash escapes

shell variable not keeping value [duplicate]

This question already has answers here:
Variables getting reset after the while read loop that reads from a pipeline
(3 answers)
Closed 7 years ago.
I have 3 lines in file /root/backuplist.txt.
The first echo prints perfectly, but the last one prints an empty line; I'm not sure why. Somehow, the $DIRS value is getting unset.
#!/bin/bash
cat /root/backuplist.txt |
while read line
do
DIRS+="$line "
echo $DIRS
done
echo $DIRS
Problem is use of pipe here, which is forking a sub-shell for your while loop and thus changes to DIRS are being made in the child shell that are not visible in the parent shell. Besides cat is unnecessary here.
Have it this way:
#!/bin/bash
while read -r line
do
DIRS+="$line "
echo "$DIRS"
done < /root/backuplist.txt
echo "$DIRS"

shell bash, how to split a string to get the text after a tag [duplicate]

This question already has answers here:
shell parsing a line to look for a certain tag
(3 answers)
Closed 8 years ago.
I am trying to split a string to get the text after a string tag. I have been told using sed might be wise, but am unsure how to implement it.
I have the following text in a file:
/#text
/#difftext
....
I basically want to loop through to and get the text after the /#
Here is what I have so far:
while read line
do
if [[ ${line} == */#" ]]
#split line to get text
done < ${FILE}
grep -Po '/#\K.*'
or
sed 's#/###'
If you already have the line in a variable you can use string substitution:
$ line='/#text /#difftext ....'
$ echo ${line##*/#}
difftext ....
while read line
do
if [[ ${line} =~ ^.*/# ]]; then
echo ${line##*#}
fi
done <file

Resources