简体   繁体   中英

Why bash converting double quotes to single quotes?

I wrote a simple test script having the below code

#!/bin/bash
CHM=test
NAME="user@test"
if ! grep -q test /etc/passwd
then
        useradd -s /bin/bash -m -d /home/test -c "${CHM} : ${NAME}" test
fi

but I'm having problem with bash converting my " to ' example output

+ CHM=test
+ NAME=user@test
+ grep -q test /etc/passwd
+ useradd -s /bin/bash -m -d /home/test -c 'test : user@test' test
useradd: invalid comment 'test : user@test'

I can't figure how to fix this any input is welcome, try to search but does not yield any preferred result

OS: RHEL6 GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu Thanks

Bash doesn't convert any quotes, this is purely an output artifact of set -x . As you can see, the ${VARIABLES} have been expanded in the set -x output. After that the quote type no longer matters and the bash maintainer chose to display all words containing blanks in single quotes to make the word boundaries clear.

The actual problem, as codegrep_admin correctly identified, is the impossibility to use a colon : for any field in /etc/passwd . Eg you can't have a user named foo:bar and you can't have a comment saying test:user@test and you can't have a shell named ba:sh .

/etc/passwd is delimited by : character and hence useradd is rejecting the comment. Try removing : in your comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM