Bash write to file without echo?

BashCommand LineIo Redirection

Bash Problem Overview


As an exercise, does a method exist to redirect a string to a file without echo? Currently I am using

echo "Hello world" > test.txt

I know about cat and printf. I was thinking something like

> test.txt <<<"Hello world"

Of course this doesnt work, but maybe a similar command?

Bash Solutions


Solution 1 - Bash

You can do this with "cat" and a here-document.

cat <<EOF > test.txt
some text
EOF

One reason for doing this would be to avoid any possibility of a password being visible in the output of ps. However, in bash and most modern shells, "echo" is a built-in command and it won't show up in ps output, so using something like this is safe (ignoring any issues with storing passwords in files, of course):

echo "$password" > test.txt

Solution 2 - Bash

I had the problem not being able to send ">" and ended up with echo!

echo "Hello world" | dd of=test.txt

Solution 3 - Bash

There are multiple ways to do it, let's run this script called exercise.sh

#!/usr/bin/env bash

> file1.txt cat <<< "This is a here-string with random value $RANDOM"

# Or if you prefer to see what is happening and write to file as well
tee file2.txt <<< "Here is another here-string I can see and write to file"

# if you want to work multiline easily
cat <<EOF > file3.txt
You don't need to escape any quotes here, $ marks start of variables, unless escaped.
This is random value from variable $RANDOM
This is literal \$RANDOM
EOF

# Let's say you have a variable with multiline text and you want to manipulate it
a="
1
2
3
33
"

# Assume I want to have lines containing "3". Instead of grep it can even be another script
a=$(echo "$a" | grep 3)

# Then you want to write this to a file, although here-string is fine,
# if you don't need single-liner command, prefer heredoc
# Herestring. (If it's single liner, variable needs to be quoted to preserve newlines)
> file4.txt cat <<< "$a"
# Heredoc
cat <<EOF > file5.txt
$a
EOF

This is the output you should see:

$ bash exercise.sh
Here is another here-string I can see and write to file

And files should contain these:

$ ls
exercise.sh  file1.txt  file2.txt  file3.txt  file4.txt  file5.txt
$ cat file1.txt
This is a here-string with random value 20914
$ cat file2.txt
Here is another here-string I can see and write to file
$ cat file3.txt
You don't need to escape any quotes here, $ marks start of variables, unless escaped.
This is random value from variable 15899
This is literal $RANDOM
$ cat file4.txt
3
33
$ cat file5.txt
3
33

Solution 4 - Bash

There are way too many ways to possibly discuss that you probably don't care about. You can hack of course - strace bash, or do all sorts of black magic running Bash in gdb.

You actually have two completely different examples there. <<<'string' is already writing a string to a file. If anything is acceptable other than printf, echo, and cat, you can use many other commands to behave like cat (sed, awk, tee, etc).

$ cp /dev/stdin ./tmpfooblah <<<'hello world'; cat tmpfooblah
hello world

Or hell, depending on how you've compiled Bash.

$ enable -f /usr/lib/bash/print print; print 'hello world' >tmpfile

If you want to use only bash strings and redirection, in pure bash, with no hacking, and no loadables, it is not possible. In ksh93 however, it is possible.

 $ rm tmpfooblah; <<<'hello world' >tmpfooblah <##@(&!()); cat tmpfooblah
 hello world

Solution 5 - Bash

The way to do this in bash is

zsh <<< '> test <<< "Hello World!"'

This is one of the interesting differences between zsh and bash: given an unchained > or >>, zsh has the good sense to hook it up to stdin, while bash does not. It would be downright useful - if it were only standard. I tried to use this to send & append my ssh key over ssh to a remote authorized_keys file, but the remote host was bash, of course, and quietly did nothing.

And that's why you should just use cat.

Solution 6 - Bash

awk ' BEGIN { print "Hello, world" } ' > test.txt

would do it

Solution 7 - Bash

I've a solution for bash purists.

The function 'define' helps us to assign a multiline value to a variable. This one takes one positional parameter: the variable name to assign the value.

In the heredoc, optionally there're parameter expansions too!

#!/bin/bash

define ()
{
  IFS=$'\n' read -r -d '' $1
}

BUCH="Matthäus 1"

define TEXT<<EOT
Aus dem Buch: ${BUCH}

1 Buch des Geschlechts Jesu Christi, des Sohnes Davids, des Sohnes Abrahams.
2 Abraham zeugte Isaak; Isaak aber zeugte Jakob, Jakob aber zeugte Juda und seine Brüder;
3 Juda aber zeugte Phares und Zara von der Thamar; Phares aber zeugte Esrom, Esrom aber zeugte Aram,

4 Aram aber zeugte Aminadab, Aminadab aber zeugte Nahasson, Nahasson aber zeugte Salmon,
5 Salmon aber zeugte Boas von der Rahab; Boas aber zeugte Obed von der Ruth; Obed aber zeugte Isai,
6 Isai aber zeugte David, den König. David aber zeugte Salomon von der, die Urias Weib gewesen; 

EOT

define TEXTNOEXPAND<<"EOT" # or define TEXTNOEXPAND<<'EOT'
Aus dem Buch: ${BUCH}

1 Buch des Geschlechts Jesu Christi, des Sohnes Davids, des Sohnes Abrahams.
2 Abraham zeugte Isaak; Isaak aber zeugte Jakob, Jakob aber zeugte Juda und seine Brüder;
3 Juda aber zeugte Phares und Zara von der Thamar; Phares aber zeugte Esrom, Esrom aber zeugte Aram,


4 Aram aber zeugte Aminadab, Aminadab aber zeugte Nahasson, Nahasson aber zeugte Salmon,
5 Salmon aber zeugte Boas von der Rahab; Boas aber zeugte Obed von der Ruth; Obed aber zeugte Isai,
6 Isai aber zeugte David, den König. David aber zeugte Salomon von der, die Urias Weib gewesen; 

EOT

OUTFILE="/tmp/matthäus_eins"

# Create file
>"$OUTFILE"

# Write contents
{
   printf "%s\n" "$TEXT"
   printf "%s\n" "$TEXTNOEXPAND"
} >>"$OUTFILE" 

Be lucky!

Solution 8 - Bash

Only redirection won't work, since there's nothing to connect the now-open file descriptors. So no, there is no way like this.

Solution 9 - Bash

Interestingly, I had this problem too...so I search and found this thread....I found that this worked well for me:

echo "Hello world" | grep "" > test.txt

However - When I had closed that terminal and opened a new one, I discovered that the problem went away! I wish I had kept that terminal open to compare the settings. My current terminal is a bash shell. Not sure what caused that issue to begin with - anyone?

Solution 10 - Bash

Old thread, but there's a useful trick for avoiding substitution which isn't mentioned in the other answers.

First off, this isn't just an academic exercise. If you're automatically or programmatically creating bash scripts then the obvious way to do it is just to echo the required commands out to the new file. However, this is next to useless, because echo substitutes far too much. You have to parse everything and escape it first, which isn't practical in some cases (supplying encrypted passwords to usermod, for example). This gets worse if you use a language which also requires string-quoting when creating the script (C++ multi-line script with backslash-newlines: outstr << "echo -e ... \\\\\n" << .../etc).

A good option is a here-doc with a quoted or escaped limit string. See the docs, example 19-7.

This won't substitute shell variables, or carry out premature command execution, and so on:

cat <<'EOF' > script.sh
...
EOF

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionanonView Question on Stackoverflow
Solution 1 - BashEricView Answer on Stackoverflow
Solution 2 - BashguestView Answer on Stackoverflow
Solution 3 - Bashuser3905644View Answer on Stackoverflow
Solution 4 - BashormaajView Answer on Stackoverflow
Solution 5 - BashpaulericksonView Answer on Stackoverflow
Solution 6 - BashDavid BurkeView Answer on Stackoverflow
Solution 7 - BashIvan GutView Answer on Stackoverflow
Solution 8 - BashIgnacio Vazquez-AbramsView Answer on Stackoverflow
Solution 9 - BashMichael BehrensView Answer on Stackoverflow
Solution 10 - BashEMLView Answer on Stackoverflow