Modify config file using bash script

A wild stab in the dark for modifying a single value: sed -c -i “s/\($TARGET_KEY *= *\).*/\1$REPLACEMENT_VALUE/” $CONFIG_FILE assuming that the target key and replacement value don’t contain any special regex characters, and that your key-value separator is “=”. Note, the -c option is system dependent and you may need to omit it for sed …

Read more

Setting environment variable for one program call in bash using env

It’s because in your first case, your current shell expands the $HELLO variable before running the commands. And there’s no HELLO variable set in your current shell. env HELLO=’Hello World’ echo $HELLO will do this: expand any variables given, in this case $HELLO run env with the 3 arguments ‘HELLO=Hello World’, ‘echo’ and ” (an …

Read more

How do I print some text in bash and pad it with spaces to a certain width?

Use – to left align a field. printf “Echoing random number %-5s [ OK ]” $RAND_NUM Alternatively, if you’re on a Red Hat Linux system there are predefined functions that will print out green OK and red FAILED prompts (the ones you see during bootup): #!/bin/bash . /etc/init.d/functions echo -n “Frobbing widget:” frob_widget && echo_success …

Read more