Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert characters Into a string in bash?

Tags:

linux

bash

I need to turn the string "125959" into "12:59:59".

Obviously, the string is the time so regular expressions aren't much good here.

like image 765
Andrew Mcdonald Avatar asked Sep 01 '12 19:09

Andrew Mcdonald


People also ask

How do I append a character to a string in bash?

Bash also allows string concatenation using the += operator. Simply a+=b can be understood as a=a+b . Here, STR2 is appended at the end of STR1 , and the result is stored in the STR1 variable.

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What is $@ in bash?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.

What does $# mean in bash?

$# is the number of positional parameters passed to the script, shell, or shell function. This is because, while a shell function is running, the positional parameters are temporarily replaced with the arguments to the function. This lets functions accept and use their own positional parameters.


1 Answers

time=125959 echo ${time:0:2}:${time:2:2}:${time:4:2} 
like image 178
Ansgar Wiechers Avatar answered Oct 03 '22 01:10

Ansgar Wiechers