简体   繁体   中英

How can I take STDIN and use it on the bash shell to set an environment variable?

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" *.

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.

export PATH=$(echo "$PATH" | sed -e "s|^/[A-Za-z/]*:||")

export is a shell built-in; that's why you can't execute it directly via xargs (there isn't an executable for export ). This runs your edit script and sets the value as the new value of $PATH . As written with double quotes around $PATH , it works even there are multiple adjacent spaces in an element of your PATH (fairly unlikely, but no harm in making sure it works properly).

This is a bit more simple and accurate:

echo "$PATH" | sed -e s/[^:]*://

The regular expression there is more accurate, it will work even if the first segment has numbers or spaces in it.

Btw, you do NOT need to export, because PATH is already exported.

The implementation of regular expressions depends on the tool. As far as I know + does not work in sed , you can do this instead:

echo "$PATH" | sed -e 's/[^:]\{1,\}://'

It seems to me that regular expressions in sed work similar to vim and different from perl .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM