Linked Questions
405 questions linked to/from Why is printf better than echo?
77
votes
6
answers
195k
views
Difference between printf and echo in bash [duplicate]
What is the difference between the printf function in bash and the echo function?
Specifically, running:
echo "blah" >/dev/udp/localhost/8125
did not send the "blah" command to the server ...
2
votes
1
answer
5k
views
How to replace `\t` with a TAB character in a string, in a way that is independent of the shell? [duplicate]
I have a script that is going to be executed by sh and bash on Debian Linux.
When it is called by sh, the following echo command works perfectly, and replaces \t with 3 spaces. However, it fails when ...
1
vote
1
answer
683
views
Why is it recommended to use "printf -- <arguments>" instead of "echo <arguments>?" [duplicate]
In the Bash Pitfalls on "Greg's Wiki" I found the following quote:
In fact, the echo command cannot be used with absolute safety here. If
the variable contains -n for example, echo will ...
0
votes
1
answer
676
views
why echo in shell script can't print -n option [duplicate]
The following script can only print -m but it can't print -n.
#!/bin/sh
echo $@
$ sh test.sh -m
-m
$ sh test.sh -n
$
0
votes
1
answer
406
views
Why does this "-e" option for echo get written to a file (although not enclosed in double quotes)? [duplicate]
#!/usr/bin/env python3
from subprocess import run
from sys import modules
def main():
doas()
def doas():
ch = input("Ready to proceed with setting up doas?[Y/n]?")
if ch == 'y':
...
0
votes
0
answers
81
views
Avoiding echo -e [duplicate]
I would like to store an HTTP request in a variable and then use it with OpenSSL
I now have
HTTP_REQUEST="HEAD / HTTP/1.1\\nHost: ${HOST_HEADER}\\nUser-Agent: check_ssl_cert/${VERSION}\\nConnection: ...
0
votes
0
answers
39
views
printf and echo -n Work Differently - Why? [duplicate]
echo -n
my_function() {
echo -n "$@"
}
my_function hello world
I get my desired output:
hello world
printf
my_function() {
printf "$@"
}
my_function hello world
I get only ...
0
votes
0
answers
37
views
echo a variable differs between the unix distributions [duplicate]
i have a simple shell script that echoes variable in a file but i have issues getting it work the same way between the different distributions.
The script
#!/bin/sh
res="=1B,\=AB=D2=DB=B5=A6e=FCj=...
0
votes
0
answers
23
views
zsh command expansion applies esacping [duplicate]
I have a string S, for which I want to replace every b character with \\ (two forward slashes).
So this is what I have:
$ S="abc"
$ A=$(echo $S | sed 's/b/\\\\/')
In bash I get the expected ...
846
votes
5
answers
392k
views
What does "--" (double dash / double hyphen) mean?
I have seen -- used in the compgen command.
For example:
compgen -W "foo bar baz" -- b
What is the meaning of the -- in there?
290
votes
21
answers
474k
views
How to add a newline to the end of a file?
Using version control systems I get annoyed at the noise when the diff says No newline at end of file.
So I was wondering: How to add a newline at the end of a file to get rid of those messages?
321
votes
12
answers
588k
views
How can I test if a variable is empty or contains only spaces?
The following bash syntax verifies if param isn't empty:
[[ ! -z $param ]]
For example:
param=""
[[ ! -z $param ]] && echo "I am not zero"
No output and its fine.
But when param is ...
138
votes
20
answers
361k
views
How to count the number of a specific character in each line?
I was wondering how to count the number of a specific character in each line by some text processing utilities?
For example, to count " in each line of the following text
"hello!"
Thank you!
The ...
90
votes
8
answers
137k
views
How to delete line with echo?
I know that I could delete the last three chars with:
echo -ne '\b\b\b'
But how can I delete a full line? I mean I don't want to use:
echo -ne '\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\...
63
votes
20
answers
43k
views
Remove duplicate $PATH entries with awk command
I am trying to write a bash shell function that will allow me to remove duplicate copies of directories from my PATH environment variable.
I was told that it is possible to achieve this with a one ...