84
votes
Accepted
Encode file content and echo it as one line
Use the -w option (line wrapping) of base64 like this:
... | base64 -w 0
A value of 0 will disable line wrapping.
52
votes
Accepted
Using `printf` to print variable containing `%` percent sign results in "bash: printf: `p': invalid format character"
Use printf in its normal form:
printf '%s\n' "${TEST}"
From man printf:
SYNOPSIS
printf FORMAT [ARGUMENT]...
You should never pass a variable to the FORMAT string as it may lead to errors and ...
50
votes
append text with echo without new line
Assuming that the file does not already end in a newline and you simply want to append some more text without adding one, you can use the -n argument, e.g.
echo -n "some text here" >> file.txt
...
45
votes
Accepted
How do you output a multi-line string that includes slashes and other special characters?
In this case, I'd use cat with a (quoted) here-document:
cat <<'END_CAT'
/\_/\
( o.o )
> ^ <
END_CAT
This is the best way of ensuring the ASCII art is outputted the way it is intended ...
43
votes
Accepted
Why is the behavior of `command 1>file.txt 2>file.txt` different from `command 1>file.txt 2>&1`?
You need to know two things:
An open file descriptor known to the application-mode side of a process references an internal kernel object known as a file description, which is an instance of an open ...
32
votes
Accepted
base64 -d decodes, but says invalid input
If you do the reverse, you'll note that the base64 string isn't complete:
$ echo '{"foo":"bar","baz":"bat"}' | base64
eyJmb28iOiJiYXIiLCJiYXoiOiJiYXQifQo=
$ ...
29
votes
Accepted
Why is echo -e behaving weird in a Makefile?
UNIX compliant implementations of echo are required to output -e<space>hello<newline>world<newline> there.
Those that don't are not compliant. Many aren't which means it's almost ...
28
votes
Is it dangerous to run echo without quotes?
Just an extra note on top of @Kusalananda's fine answer.
echo run after_bundle
is fine because none of the characters in those 3 arguments¹ passed to echo contain characters that are special to the ...
26
votes
Accepted
How do I print the nth argument of a script, when n isn't known until runtime?
By chronological order, in various shells:
csh (late 70s): $argv[$n] (also works in zsh since 1991 and fish since 2005)
zsh (1991): $argv[n] / $@[n] / $*[n] (the last too also supported by yash but ...
25
votes
How do I set an environment variable on the command line and have it appear in commands?
You're doing it correctly, but the bash syntax is easy to misinterpret: you could think that echo $TEST causes echo to fetch TEST env var then print it, it does not. So given
export TEST=123
then
...
25
votes
append text with echo without new line
echo "abc" >>file.txt puts a newline after abc, not before. If you end up with abc on its own line, that means that the newline before abc was already present in file.txt.
Note that it is ...
24
votes
Accepted
Is it possible to print the content of the content of a variable with shell script? (indirect referencing)
You can accomplish this using bash's indirect variable expansion (as long as it's okay for you to leave out the $ from your reference variable):
$ var=test
$ test="my string"
$ echo "$var"
test
$ ...
23
votes
Accepted
Is it dangerous to run echo without quotes?
For the specific case
echo run after_bundle
quoting is not needed. No quoting is needed because the argument to echo are static strings that contain no variable expansions or command substitutions ...
23
votes
Accepted
Only print output after finding pattern
AWK can do this with pattern ranges, which allows the use of any regular expression:
echoer | awk '/pattern/,0'
will print echoer’s output starting with the first line matching pattern.
AWK is ...
22
votes
Accepted
Echoing a tail command produces unexpected output?
Your crontab line has one or more asterisks * in it, indicating "any time". When that line is substituted in from the command substitution, the result is something like
echo * * * * * cmd > /path/...
22
votes
Accepted
Why does echo > 1 2 3 create a file called 1 with contents 2 3?
A proper resource you can refer to is the POSIX shell grammar, which defines a simple command as:
simple_command : cmd_prefix cmd_word cmd_suffix
| cmd_prefix cmd_word
...
21
votes
Accepted
Why does `md5sum` not give the same hash as the Internet does?
The reason for the hashes being different is that echo includes a newline at the end of the output string to make it pretty. This can be prohibited by the -n flag (if your implementation of echo ...
18
votes
append text with echo without new line
I don't think it's possible with echo command, use the following sed approach instead:
sed -i '$ s/$/abc/' file.txt
-i - modify the file inplace
$ - indicate the last record/line
s/$/abc/ - ...
18
votes
Accepted
Print one file per line using echo
echo can't be used to output arbitrary data anyway, use printf instead which is the POSIX replacement for the broken echo utility to output text.
printf '%s\n' small*jpg
You can also do:
printf '%s\...
17
votes
base64 -d decodes, but says invalid input
The command-line tools are picky about the proper amount of padding characters to make the input length a multiple of four. That string is 34 characters long, so there should be two = signs as padding ...
17
votes
How do I print the nth argument of a script, when n isn't known until runtime?
The arguments you pass to a script are stored in the @ array, so the simple way to do what you want is to take an array slice, starting at position n and having a length of 1:
#!/bin/bash
n=3
echo &...
16
votes
Why is the behavior of `command 1>file.txt 2>file.txt` different from `command 1>file.txt 2>&1`?
Using > tells it to overwrite the file. Since you have stdout and stderr writing to the file in two different operations, the last one to write will overwrite the first.
You can do:
command 1>...
16
votes
How do you output a multi-line string that includes slashes and other special characters?
Quote it with single '' or double quotes "" to prevent the <>() from being taken as operators. Then if you want it on one line, use echo -e or $' ' in Bash, mark the newlines as \n and ...
16
votes
Accepted
Why does /bin/echo cause SIGPIPE, while Bash's built-in echo does not?
In
{ echo 1 || >&2 echo "[ $? ]" ; } | echo
{ echo 1 || >&2 echo "[ $? ]" ; } and echo have to run concurrently, so in separate processes.
The echo 1 || >&2 ...
15
votes
netcat: send text to echo service, read reply then exit
I know this is a bit old but no other answer worked for me and this did:
echo 'test' | netcat -N $server $port
Notice the -N:
shutdown the network socket after EOF on the input. Some servers ...
15
votes
Echo is not printing the correct value for a variable
You have not quoted the variable expansion in your echo, and you have a file called a in the current directory.
The [{nodelay,true}] acts like a filename globbing pattern that will match any file ...
15
votes
Temporarily unset bash option -x
You can achieve this in Bash by not using set -x and instead trapping DEBUG and doing your own tracing:
#!/bin/bash
set -T
trap '! [[ "$BASH_COMMAND" =~ ^(echo|printf) ]] &&
...
14
votes
Accepted
Using tab delimiter in Cut in Unix Shell Scripting
cut -f 1 input.txt
This gives you the first column from the tab-delimited file input.txt. The default field delimiter for cut is the tab character, so there's no need to further specify this.
If the ...
14
votes
base64 -d decodes, but says invalid input
GNU's base64 -d requires proper padding (input length must be a multiple of 4). Other base64 decoders may be smarter and not require padding (e.g. Mac/BSD base64 -D does not require padding).
Here's a ...
13
votes
How to delete line with echo?
Here's an example using echo's no newline -n and carriage return \r options.
#!/bin/bash
CHECK_MARK="\033[0;32m\xE2\x9C\x94\033[0m"
echo -e "\n\e[4mDoing Things\e[0m"
echo -n "doing thing 1..."
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
echo × 518bash × 211
shell × 99
shell-script × 94
linux × 38
variable × 38
printf × 37
quoting × 26
io-redirection × 25
pipe × 22
sed × 20
escape-characters × 20
cat × 19
scripting × 17
text-processing × 16
newlines × 15
command-line × 13
xargs × 12
stdout × 12
files × 11
wildcards × 11
string × 11
command-substitution × 11
read × 10
grep × 9