Linux - Bash Redirect a String to a file
You need to wrap your variables in double quotes:
echo "$newexpr" > "$path/$file"
The quotes around $path/$file
aren't actually necessary in this case but they do no harm.
More generally, you should also use $( )
rather than backticks:
newexpr=$(awk '/^Build Number/{$4=$4+1;}1' "$path/$file")
If you want to achieve the effect of changing the file "in-place", you don't need to use a variable. You can use a temporary file like this:
awk '/^Build Number/{$4=$4+1;}1' "$path/$file" > /tmp/file && mv /tmp/file "$path/$file"
The importance of using quotes
The double quotes preserve the original format of the data. See this simple example, which uses set -x
to activate debug mode. The commands that are being executed by the shell are shown on the lines beginning with +
. Actually I see that you're already using #!/bin/bash -x
. set -x
does the same thing as that.:
$ s="1
> 2"
$ set -x
$ echo $s
+ echo 1 2
1 2
$ echo "$s"
+ echo '1
2'
1
2
The original string contains a newline but when you echo
it without quotes, it is interpreted as two arguments to echo
, instead of one argument that contains a newline. This is called field splitting. You can learn more about the importance of using double quotes by reading this this wiki article.
Shell Script: How to write a string to file and to stdout on console?
Use the tee
command:
echo "hello" | tee logfile.txt
bash: the difference between and redirect
<
is used to redirect from a file. So < "$list"
looks for a file whose name is the value of the list
variable, and tries to read from that file.
<<<
creates a here-string, <<< "$list"
reads directly from the value of the variable.
How can I use a file in a command and redirect output to the same file without truncating it?
You cannot do that because bash processes the redirections first, then executes the command. So by the time grep looks at file_name, it is already empty. You can use a temporary file though.
#!/bin/sh
tmpfile=$(mktemp)
grep -v 'seg[0-9]\{1,\}\.[0-9]\{1\}' file_name > ${tmpfile}
cat ${tmpfile} > file_name
rm -f ${tmpfile}
like that, consider using mktemp
to create the tmpfile but note that it's not POSIX.
How to redirect and append both standard output and standard error to a file with Bash
cmd >>file.txt 2>&1
Bash executes the redirects from left to right as follows:
>>file.txt
: Openfile.txt
in append mode and redirectstdout
there.2>&1
: Redirectstderr
to "wherestdout
is currently going". In this case, that is a file opened in append mode. In other words, the&1
reuses the file descriptor whichstdout
currently uses.
How to redirect output to a file and stdout
The command you want is named tee
:
foo | tee output.file
For example, if you only care about stdout:
ls -a | tee output.file
If you want to include stderr, do:
program [arguments...] 2>&1 | tee outfile
2>&1
redirects channel 2 (stderr/standard error) into channel 1 (stdout/standard output), such that both is written as stdout. It is also directed to the given output file as of the tee
command.
Furthermore, if you want to append to the log file, use tee -a
as:
program [arguments...] 2>&1 | tee -a outfile
How to redirect stdin to file in bash
I don't think there is a builtin that reads from stdin until EOF, but you can do this:
#!/bin/bash
exec > /tmp/file
while IFS= read -r line; do
printf '%s\n' "$line"
done
Related Topics
Linux Script Telnet Head Request
Few Shell Commands Doesn't Work When I Invoke a Script via Qprocess in Qt
Docker Non-Root Bind-Mount Permissions, with -Userns-Remap
How Does Apparmor Handle Linux-Kernel Mount Namespaces
Bash - Ignore Hidden Files and Empty Source Directory When Copying
How to Trigger a Function in Kernel Module Interrupt
How to Use Xdotool to Enter a Web Console Command
Cpu Usage from Linux Then Using It in a Arithmetic Expression
How to Delay Pipe Netcat to Connect on First Input
What's The Relation Between 32/64-Bit Application, Os and Processor
Restart Service from Cgi Script
How to Create a Folder with a Folder Name Containing Spaces in Linux
How to Find/Cut for Only The Filename from an Output of Ls -Lrt in Perl
Perl Script to Capture Stderr and Stdout of Command Executed in Back-Quotes
How to Source a Simple Bash Script
How to Do an Initial Setup of Slapd Olc with Ldapmodify