Linux - Bash Redirect a String to a File

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:

  1. >>file.txt: Open file.txt in append mode and redirect stdout there.
  2. 2>&1: Redirect stderr to "where stdout is currently going". In this case, that is a file opened in append mode. In other words, the &1 reuses the file descriptor which stdout 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



Leave a reply



Submit