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.
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.
$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.
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.
$# 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.
time=125959 echo ${time:0:2}:${time:2:2}:${time:4:2}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With