Linux repeat string n times in command line
Method 1 : Use command Yes
j@ubuntu2004:~$ yes FOO | head -5 FOO FOO FOO FOO FOO j@ubuntu2004:~$ yes FOO | head -5 | xargs FOO FOO FOO FOO FOO j@ubuntu2004:~$
yes - output a string repeatedly until killed
xargs - replace new line with space
Method 2: use printf the print n space then replace the with your word
j@ubuntu2004:~$ printf "%5s" |sed 's/ /FOO /g' FOO FOO FOO FOO FOO j@ubuntu2004:~$ j@ubuntu2004:~$ n=4 j@ubuntu2004:~$ printf "%${n}s" |sed 's/ /FOO /g' FOO FOO FOO FOO j@ubuntu2004:~$
Method 3: Use command “seq” then replace the numbers with your foo
j@ubuntu2004:~$ seq 5 1 2 3 4 5 j@ubuntu2004:~$ seq 5 | sed "c FOO" FOO FOO FOO FOO FOO j@ubuntu2004:~$ seq 5 | sed "c FOO" | xargs FOO FOO FOO FOO FOO j@ubuntu2004:~$ j@ubuntu2004:~$ seq 5 | xargs -I -- echo "FOO" FOO FOO FOO FOO FOO
Method 4: Use for loop
j@ubuntu2004:~$ for i in {1..4}; do echo FOO;done FOO FOO FOO FOO j@ubuntu2004:~$ for i in {1..4}; do echo -n "FOO ";done FOO FOO FOO FOO j@ubuntu2004:~$
Note: For {1..4} there is no space