0

So I have a configuration file that I want to change after receiving a prompted response from the user.

read -p 'Your RPC Username: ' RPC_USER
sleep 1s
echo -e "${YELLOW}"
echo "$RPC_USER"
echo "----------------"
echo "Is this correct?"
echo -e "${RED}"
select yn in "Yes" "No"; do
    case $yn in
        Yes ) break;;
        No ) read -p 'New RPC Username: ' RPC_USER
            echo "$RPC_USER"
    esac
done

After this I want to change a file configuration say: test.conf. So I try:

sed -i '1i RPC_USERNAME="$RPC_USER"' ~/test.conf

But only thing I get is:

RPC_USERNAME="$RPC_USER"

Again this is to be able to paste these user input variables directly into the configuration file for them during the script.

Any help is greatly appreciated!

1 Answers1

0

Variables are not resolving inside single quotes. You have to use double quotes.

Or insert variable directly into command:

read -p 'Your RPC Username: ' RPC_USER
sleep 1s
echo -e "${YELLOW}"
echo "$RPC_USER"
echo "----------------"
echo "Is this correct?"
echo -e "${RED}"
select yn in "Yes" "No"; do
    case $yn in
        Yes ) break;;
        No ) read -p 'New RPC Username: ' RPC_USER
            echo "$RPC_USER"
            sed -i '1i RPC_USERNAME="'$RPC_USER'"' ~/test.conf
    esac
done

But be aware, that's not secure, because if user enters name with single quote - it will brake command. So you still need to escape it

Alex Kapustin
  • 1,869
  • 12
  • 15