Search code examples
bashsedhome-directory

sed not able to process ~user/.bash_profile


I have a pretty strange problem with sed, if I do with this:

[root@Camel ~]-> sed -i 's/TLRAGENT_IP=.*/TLRAGENT_IP='"${HOST_IP}"'/' ~user/.bash_profile

it's fine. But if I try the following:

[root@Camel ~]-> CONF_FILE="~user/.bash_profile"
[root@Camel ~]-> sed -i 's/TLRAGENT_IP=.*/TLRAGENT_IP='"${HOST_IP}"'/' ${CONF_FILE}
sed: can't read ~user/.bash_profile: No such file or directory

also tried to quote the variable:

[root@Camel ~]-> sed -i 's/TLRAGENT_IP=.*/TLRAGENT_IP='"${HOST_IP}"'/' "${CONF_FILE}"
sed: can't read ~user/.bash_profile: No such file or directory

couldn't figure out where went wrong, please advise.

Shell's version is 3.2.25(1)-release.


Solution

  • from man bash /EXPANSION

    EXPANSION
     Expansion is performed on the command line after it has been
     split  into  words.  There are seven kinds of expansion per-
     formed:  brace expansion,  tilde  expansion,  parameter  and
     variable  expansion, command substitution, arithmetic expan-
     sion, word splitting, and pathname expansion.
    
     The order of expansions is: brace  expansion,  tilde  expan-
     sion,  parameter, variable and arithmetic expansion and com-
     mand substitution (done in a  left-to-right  fashion),  word
     splitting, and pathname expansion.
    

    The parameter and variable expansion comes after tilde expansion

    To have tilde expansion, this can be done at variable definition

    CONF_FILE=~user/.bash_profile
    

    instead of

    CONF_FILE="~user/.bash_profile"