2

I am trying to capture the currently logged in user on the linux box into a variable and then use the variable to insert it into a text file and replace the username in that file

in the file (live.conf) i want to replace the line : "live_user=whatever" with "live_user=myvariable"

My script looks like this :

#!/usr/bin/env bash

username=${USER:=$(/usr/bin/id -run)}

sed -i -r 's/^#?(live_user)\s*=.*/\1=$username/' live.conf

exit 0

however the result is it replaces the line now to look like this :

"live_user=$username" and not the actual variable...

Anyone have any ideas on how i can make this script work ?

1 Answers1

0

Found the problem, using single quotes... i was meant to use double quotes as below

#!/usr/bin/env bash

username=${USER:=$(/usr/bin/id -run)}

sed -i -r "s/^#?(live_user)\s*=.*/\1=$username" live.conf

exit 0

it works now.