Ubuntu – Can’t pass the variable into other command

bashcommand lineenvironment-variables

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.

Best Answer

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]