How do I assign a value to a variable using cut?

Below is the command I use to assign "yellow" to the variable yellow. I may seem like you are echoing using xargs, but when I assign it to yellow and then try to echo it, it prints an empty line.

Below is the command. Your help is greatly appreciated!

cut -c 2- color.txt | xargs -I  {}  yellow={};

      

+3


source to share


3 answers


No need for xargs

here:

yellow=$(cut -c 2- color.txt)

      

Since it xargs

runs as a subprocess, you cannot actually do anything that changes the state of your shell, even if you start a new shell, the shell variables and other local state will disappear after it exits. Therefore, shell assignments do not make sense as subcommands passed to xargs.




However, you really don't need to cut

. In native bash, without using subprocesses or external tools:

read color <color.txt
yellow=${color:1}

      

( 1

- the same column that was 2

cut like bash PE expressions start with the first character as 0

).

+4


source


Use command replacement :

yellow=$(cut -c 2- color.txt)

      



The syntax is $()

extended to the output of the command, which is then assigned to a variable.

+4


source


You need to use:

yellow=$(cut -c 2- color.txt)

      

xargs

an external shell binary needs to be executed, yellow={}

not a binary.

+3


source







All Articles