5

I was trying to pass a variable another shell command.

To be clear;

passwdContent=$( getent passwd "$USER" )
echo "passwdcontent" $passwdContent

I get the "passwd" field from user "x". After this section I want to play with this variable.

But in here I couldn't solve how can I pass the "passwdContent" variable. I tried a way like this;

exampleVariable=[( "$passwdContent" | cut -d : -f 1 )]

After I want to echo the "exampleVariable" it gave me error.

I knew I can do this all of in one line. But I want to learn how can I do this.

0

2 Answers 2

4

You should use command substitution and for doing that you are missing a $.

You should also pipe the content of your variable to cut or read it from stdin.

So use this instead:

$ exampleVariable="[$(cut -d: -f1 <<<$passwdContent )]"

Now:

$ echo "$exampleVariable"
[ravexina]
0
3

Another way to parse the output from getent.

$ [[ $(getent passwd "$USER") =~ ([^:]+):x: ]] && var=${BASH_REMATCH[1]}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .