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={};
source to share
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
).
source to share
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.
source to share