How to repeat a dash (hyphen) in shell
This throws an error:
$ printf '-%.0s' {1..100}; echo ""
bash: printf: -%: invalid option
printf: usage: printf [-v var] format [arguments]
This works fine under bash
:
$ printf -- '-%.0s' {1..100}; echo ""
----------------------------------------------------------------------------------------------------
For other shells, try:
printf -- '-%.0s' $(seq 100); echo ""
The problem was the printf
expects that -
starts an option. As is common among Unix/POSIX utilities in this type of situation, --
signals to printf
to expect no more options.
How can I repeat a character in Bash?
You can use:
printf '=%.0s' {1..100}
How this works:
Bash expands {1..100} so the command becomes:
printf '=%.0s' 1 2 3 4 ... 100
I've set printf's format to =%.0s
which means that it will always print a single =
no matter what argument it is given. Therefore it prints 100 =
s.
How can I repeat a character in Bash?
You can use:
printf '=%.0s' {1..100}
How this works:
Bash expands {1..100} so the command becomes:
printf '=%.0s' 1 2 3 4 ... 100
I've set printf's format to =%.0s
which means that it will always print a single =
no matter what argument it is given. Therefore it prints 100 =
s.
How to include dashes in the format string of bash printf?
Always specify end of command line flags when using strings involving --
. In general the shell commands need to know where their positional argument start at. So by forcing --
after printf
we let it know that the subsequent arguments are to be interpreted as its arguments. At this point, afterwards using --
will be treated literally instead of being considered as a command line switch.
so define your printf
as
printf -- "--file=%s " "hello printf" "in" "bash script"
Also if you are planning to specify multiple printf
argument strings,do not include them in same format specifier. You might need this
printf -- "--file=%s %s %s" "hello printf" "in" "bash script"
See more on The printf command in bash
Print a character repeatedly in bash
sure, just use printf
and a bit of bash string manipulation
$ s=$(printf "%-30s" "*")
$ echo "${s// /*}"
******************************
There should be a shorter way, but currently that's how i would do it. You can make this into a function which you can store in a library for future use
printf_new() {
str=$1
num=$2
v=$(printf "%-${num}s" "$str")
echo "${v// /*}"
}
Test run:
$ printf_new "*" 20
********************
$ printf_new "*" 10
**********
$ printf_new "%" 10
%%%%%%%%%%
Multiplying strings in bash script
You can use bash
command substitution
to be more portable across systems than to use a variant specific command.
$ myString=$(printf "%10s");echo ${myString// /m} # echoes 'm' 10 times
mmmmmmmmmm
$ myString=$(printf "%10s");echo ${myString// /rep} # echoes 'rep' 10 times
reprepreprepreprepreprepreprep
Wrapping it up in a more usable shell-function
repeatChar() {
local input="$1"
local count="$2"
printf -v myString '%*s' "$count"
printf '%s\n' "${myString// /$input}"
}
$ repeatChar str 10
strstrstrstrstrstrstrstrstrstr
Related Topics
How to Pipe Output to a File When Running as a Systemd Service
Is Clock_Gettime() Adequate for Submicrosecond Timing
Shell Script Working Fine Without Shebang Line? Why
Null Modem Emulator (Com0Com) for Linux
Unsupported Protocol While Download Tar.Gz Package
Retrieve Plain Text Script from Compiled Bash Script
How to Redirect from Audio Output to Mic Input Using Pulseaudio
Redirect Process Stdin and Stdout to Netcat
Effects of Removing All Symbol Table and Relocation Information from an Executable
How to Split a Ca Certificate Bundle into Separate Files
Get the Characters After the Last Index of a Substring from a String
Interrupting Epoll_Wait with a Non-Io Event, No Signals
Ldd Doesn't Work on Dynamically Linked Binary
Compiling a Linux Program for Arm Architecture - Running on a Host Os
Compile Swift Script with Static Swift Core Library
Cannot --Enable-Pcregrep-Libbz2 Because Bzlib.H Was Not Found