How can I take STDIN and use it on the bash shell to set an environment variable?
bash, env, path, sed, xargs
Solution
I think the best solution for that particular problem is:
export PATH=${PATH#*:}
But that doesn't answer the question of how to use `stdin` to set an environment variable.
It's easy to use some process' `stdout`:
export PATH=$(make_new_path)
But that's not strictly speaking using `stdin`. The obvious solution:
make_new_path | export PATH=$(cat)
won't work because the commands to the right of the pipe are executed in a subshell, so variable settings won't stick. (If it had worked, it would have been an UUOC.)
As Gordon Davisson points out in a comment, you could simply use:
read -r PATH; export PATH
Also, it is actually not necessary to `export PATH`, since `PATH` comes pre-exported, but I left it in for the case where you want to use some other environment variable.
Problem
I'm trying to get better at one-liners here, this is what I have currently. ``` $ echo $PATH /home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games ``` What I want to do is strip off that first chunk, so here's what I've figured out so far --- ``` $ echo $PATH | sed -e "s|^/[A-Za-z/]*:||" /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games ``` The last thing I want to do here is to put that back into PATH, like export PATH=[that result] The way I've tried this was as follows, which I can't get to work --- ``` $ echo $PATH | sed -e "s|^/[A-Za-z/]*:||" | xargs export PATH= xargs: export: No such file or directory ``` Also, BTW, separate issue that confused me was with the sed expression above, for some reason when I tried using + rather than *, it wouldn't catch the first bit. Look --- ``` $ echo $PATH | sed -e "s|^/[A-Za-z/]+:||" /home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games ``` Its like the +, meaning "one or more" doesn't work as well as the "many" *.