How to remove first segment or segments from PATH in shell script [duplicate]












0
















This question already has an answer here:




  • How can I make environment variables “exported” in a shell script stick around?

    2 answers



  • How can I set environment variable permanently through shell script? [duplicate]

    1 answer




I wanted to remove a CUDA directory from PATH by a sh script. The directories are showed by doing echo $PATH



/usr/local/cuda-9.0/bin:/usr/sbin:/usr/bin:


I want only:



/usr/sbin:/usr/bin:


My script changes nothing but it DOES in terminal(this script will be automatically executed when I do conda deactivate):



export PATH=$(echo ${PATH} | sed -r 's|/usr/local/cuda-9.0/bin||')


Could somebody kindly tell me why and correct it into an elegant way? Thanks










share|improve this question















marked as duplicate by l0b0, RalfFriedl, JigglyNaga, elbarna, penguin359 Jan 5 at 0:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1





    If you change PATH within a script it won't change the value of PATH in the shell you are using to call the script.

    – nohillside
    Jan 3 at 10:02











  • To remove this first segment without sed: PATH=${PATH#/usr/local/cuda-9.0/bin:}.

    – Kamil Maciorowski
    Jan 3 at 10:05













  • @nohillside Is there a way to do this in a script?

    – Zeliang Su
    Jan 3 at 10:09













  • See unix.stackexchange.com/questions/42223/…

    – nohillside
    Jan 3 at 10:13











  • @nohillside I write this .sh which runs when I shut down a virtual env of conda. So I want it source automatically

    – Zeliang Su
    Jan 3 at 12:54
















0
















This question already has an answer here:




  • How can I make environment variables “exported” in a shell script stick around?

    2 answers



  • How can I set environment variable permanently through shell script? [duplicate]

    1 answer




I wanted to remove a CUDA directory from PATH by a sh script. The directories are showed by doing echo $PATH



/usr/local/cuda-9.0/bin:/usr/sbin:/usr/bin:


I want only:



/usr/sbin:/usr/bin:


My script changes nothing but it DOES in terminal(this script will be automatically executed when I do conda deactivate):



export PATH=$(echo ${PATH} | sed -r 's|/usr/local/cuda-9.0/bin||')


Could somebody kindly tell me why and correct it into an elegant way? Thanks










share|improve this question















marked as duplicate by l0b0, RalfFriedl, JigglyNaga, elbarna, penguin359 Jan 5 at 0:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1





    If you change PATH within a script it won't change the value of PATH in the shell you are using to call the script.

    – nohillside
    Jan 3 at 10:02











  • To remove this first segment without sed: PATH=${PATH#/usr/local/cuda-9.0/bin:}.

    – Kamil Maciorowski
    Jan 3 at 10:05













  • @nohillside Is there a way to do this in a script?

    – Zeliang Su
    Jan 3 at 10:09













  • See unix.stackexchange.com/questions/42223/…

    – nohillside
    Jan 3 at 10:13











  • @nohillside I write this .sh which runs when I shut down a virtual env of conda. So I want it source automatically

    – Zeliang Su
    Jan 3 at 12:54














0












0








0









This question already has an answer here:




  • How can I make environment variables “exported” in a shell script stick around?

    2 answers



  • How can I set environment variable permanently through shell script? [duplicate]

    1 answer




I wanted to remove a CUDA directory from PATH by a sh script. The directories are showed by doing echo $PATH



/usr/local/cuda-9.0/bin:/usr/sbin:/usr/bin:


I want only:



/usr/sbin:/usr/bin:


My script changes nothing but it DOES in terminal(this script will be automatically executed when I do conda deactivate):



export PATH=$(echo ${PATH} | sed -r 's|/usr/local/cuda-9.0/bin||')


Could somebody kindly tell me why and correct it into an elegant way? Thanks










share|improve this question

















This question already has an answer here:




  • How can I make environment variables “exported” in a shell script stick around?

    2 answers



  • How can I set environment variable permanently through shell script? [duplicate]

    1 answer




I wanted to remove a CUDA directory from PATH by a sh script. The directories are showed by doing echo $PATH



/usr/local/cuda-9.0/bin:/usr/sbin:/usr/bin:


I want only:



/usr/sbin:/usr/bin:


My script changes nothing but it DOES in terminal(this script will be automatically executed when I do conda deactivate):



export PATH=$(echo ${PATH} | sed -r 's|/usr/local/cuda-9.0/bin||')


Could somebody kindly tell me why and correct it into an elegant way? Thanks





This question already has an answer here:




  • How can I make environment variables “exported” in a shell script stick around?

    2 answers



  • How can I set environment variable permanently through shell script? [duplicate]

    1 answer








shell-script bashrc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 10:10







Zeliang Su

















asked Jan 3 at 9:54









Zeliang SuZeliang Su

11




11




marked as duplicate by l0b0, RalfFriedl, JigglyNaga, elbarna, penguin359 Jan 5 at 0:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by l0b0, RalfFriedl, JigglyNaga, elbarna, penguin359 Jan 5 at 0:21


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    If you change PATH within a script it won't change the value of PATH in the shell you are using to call the script.

    – nohillside
    Jan 3 at 10:02











  • To remove this first segment without sed: PATH=${PATH#/usr/local/cuda-9.0/bin:}.

    – Kamil Maciorowski
    Jan 3 at 10:05













  • @nohillside Is there a way to do this in a script?

    – Zeliang Su
    Jan 3 at 10:09













  • See unix.stackexchange.com/questions/42223/…

    – nohillside
    Jan 3 at 10:13











  • @nohillside I write this .sh which runs when I shut down a virtual env of conda. So I want it source automatically

    – Zeliang Su
    Jan 3 at 12:54














  • 1





    If you change PATH within a script it won't change the value of PATH in the shell you are using to call the script.

    – nohillside
    Jan 3 at 10:02











  • To remove this first segment without sed: PATH=${PATH#/usr/local/cuda-9.0/bin:}.

    – Kamil Maciorowski
    Jan 3 at 10:05













  • @nohillside Is there a way to do this in a script?

    – Zeliang Su
    Jan 3 at 10:09













  • See unix.stackexchange.com/questions/42223/…

    – nohillside
    Jan 3 at 10:13











  • @nohillside I write this .sh which runs when I shut down a virtual env of conda. So I want it source automatically

    – Zeliang Su
    Jan 3 at 12:54








1




1





If you change PATH within a script it won't change the value of PATH in the shell you are using to call the script.

– nohillside
Jan 3 at 10:02





If you change PATH within a script it won't change the value of PATH in the shell you are using to call the script.

– nohillside
Jan 3 at 10:02













To remove this first segment without sed: PATH=${PATH#/usr/local/cuda-9.0/bin:}.

– Kamil Maciorowski
Jan 3 at 10:05







To remove this first segment without sed: PATH=${PATH#/usr/local/cuda-9.0/bin:}.

– Kamil Maciorowski
Jan 3 at 10:05















@nohillside Is there a way to do this in a script?

– Zeliang Su
Jan 3 at 10:09







@nohillside Is there a way to do this in a script?

– Zeliang Su
Jan 3 at 10:09















See unix.stackexchange.com/questions/42223/…

– nohillside
Jan 3 at 10:13





See unix.stackexchange.com/questions/42223/…

– nohillside
Jan 3 at 10:13













@nohillside I write this .sh which runs when I shut down a virtual env of conda. So I want it source automatically

– Zeliang Su
Jan 3 at 12:54





@nohillside I write this .sh which runs when I shut down a virtual env of conda. So I want it source automatically

– Zeliang Su
Jan 3 at 12:54










2 Answers
2






active

oldest

votes


















0














You could simply export PATH=/usr/sbin:/usr/bin. But your method already works:



$ PATH=/usr/local/cuda-9.0/bin:/usr/sbin:/usr/bin:
$ export PATH=$(echo ${PATH} | sed -r 's|/usr/local/cuda-9.0/bin||')
$ echo $PATH
:/usr/sbin:/usr/bin:


Trying to set PATH within your current environment by running a script is not possible.






share|improve this answer


























  • It works in terminal but not in script. Could you please edit it into the form for a script please?

    – Zeliang Su
    Jan 3 at 10:04



















0














export PATH=$(echo ${PATH} | sed -r 's|/usr/local/cuda-9.0/bin||')


Or its more correct version:



export PATH="$(printf '%sn' "$PATH" | sed 's|/usr/local/cuda-9.0/bin||')"


Updates the content of the $PATH variable of the shell invoking that command by removing the first occurrence of /usr/local/cuda-9.0/bin in it. For instance, it would change a /bin:/opt/usr/local/cuda-9.0/bin/v2:/usr/local/cuda-9.0/bin to /bin:/opt/v2:/usr/local/cuda-9.0/bin which is not what you want.



To remove full elements, you could do:



export PATH="$(
printf ':%s:n' "$PATH" |
sed '
:1
s|:/usr/local/cuda-9.0/bin:|:|g; t1
s|^:||; s|:$|'
)"


Or with zsh:



path=("${path[@]:#/usr/local/cuda-9.0/bin}")


Note that if $PATH initially only contained /usr/local/cuda-9.0/bin, then the end result will be an empty $PATH. And empty $PATH means search for executables in the current directory which is not what you want. If that is a possibility, then you may want to make $PATH something like /inexistent/to/disable/PATH/lookup.



Note that in any case, it only changes the $PATH variable of the shell that runs that code. If you wanted that to affect the $PATH variable of the shell that invokes that script, you'd need that shell to source (as with the . or source command) that deactivate script, or make deactivate a function of that shell.



Or alternatively, you can have the deactivate script output the shell code that would be needed to remove those entries from $PATH (for instance by adding export -p PATH after having modified it) and make sure to invoke it as:



eval "$(deactivate)"


Within the shell where you want that entry to be removed from $PATH.



Something like:



#! /bin/zsh -
path=("${path[@]:#/usr/local/cuda-9.0/bin}")
export -p PATH





share|improve this answer
































    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You could simply export PATH=/usr/sbin:/usr/bin. But your method already works:



    $ PATH=/usr/local/cuda-9.0/bin:/usr/sbin:/usr/bin:
    $ export PATH=$(echo ${PATH} | sed -r 's|/usr/local/cuda-9.0/bin||')
    $ echo $PATH
    :/usr/sbin:/usr/bin:


    Trying to set PATH within your current environment by running a script is not possible.






    share|improve this answer


























    • It works in terminal but not in script. Could you please edit it into the form for a script please?

      – Zeliang Su
      Jan 3 at 10:04
















    0














    You could simply export PATH=/usr/sbin:/usr/bin. But your method already works:



    $ PATH=/usr/local/cuda-9.0/bin:/usr/sbin:/usr/bin:
    $ export PATH=$(echo ${PATH} | sed -r 's|/usr/local/cuda-9.0/bin||')
    $ echo $PATH
    :/usr/sbin:/usr/bin:


    Trying to set PATH within your current environment by running a script is not possible.






    share|improve this answer


























    • It works in terminal but not in script. Could you please edit it into the form for a script please?

      – Zeliang Su
      Jan 3 at 10:04














    0












    0








    0







    You could simply export PATH=/usr/sbin:/usr/bin. But your method already works:



    $ PATH=/usr/local/cuda-9.0/bin:/usr/sbin:/usr/bin:
    $ export PATH=$(echo ${PATH} | sed -r 's|/usr/local/cuda-9.0/bin||')
    $ echo $PATH
    :/usr/sbin:/usr/bin:


    Trying to set PATH within your current environment by running a script is not possible.






    share|improve this answer















    You could simply export PATH=/usr/sbin:/usr/bin. But your method already works:



    $ PATH=/usr/local/cuda-9.0/bin:/usr/sbin:/usr/bin:
    $ export PATH=$(echo ${PATH} | sed -r 's|/usr/local/cuda-9.0/bin||')
    $ echo $PATH
    :/usr/sbin:/usr/bin:


    Trying to set PATH within your current environment by running a script is not possible.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 3 at 10:04

























    answered Jan 3 at 10:01









    l0b0l0b0

    27.7k17115243




    27.7k17115243













    • It works in terminal but not in script. Could you please edit it into the form for a script please?

      – Zeliang Su
      Jan 3 at 10:04



















    • It works in terminal but not in script. Could you please edit it into the form for a script please?

      – Zeliang Su
      Jan 3 at 10:04

















    It works in terminal but not in script. Could you please edit it into the form for a script please?

    – Zeliang Su
    Jan 3 at 10:04





    It works in terminal but not in script. Could you please edit it into the form for a script please?

    – Zeliang Su
    Jan 3 at 10:04













    0














    export PATH=$(echo ${PATH} | sed -r 's|/usr/local/cuda-9.0/bin||')


    Or its more correct version:



    export PATH="$(printf '%sn' "$PATH" | sed 's|/usr/local/cuda-9.0/bin||')"


    Updates the content of the $PATH variable of the shell invoking that command by removing the first occurrence of /usr/local/cuda-9.0/bin in it. For instance, it would change a /bin:/opt/usr/local/cuda-9.0/bin/v2:/usr/local/cuda-9.0/bin to /bin:/opt/v2:/usr/local/cuda-9.0/bin which is not what you want.



    To remove full elements, you could do:



    export PATH="$(
    printf ':%s:n' "$PATH" |
    sed '
    :1
    s|:/usr/local/cuda-9.0/bin:|:|g; t1
    s|^:||; s|:$|'
    )"


    Or with zsh:



    path=("${path[@]:#/usr/local/cuda-9.0/bin}")


    Note that if $PATH initially only contained /usr/local/cuda-9.0/bin, then the end result will be an empty $PATH. And empty $PATH means search for executables in the current directory which is not what you want. If that is a possibility, then you may want to make $PATH something like /inexistent/to/disable/PATH/lookup.



    Note that in any case, it only changes the $PATH variable of the shell that runs that code. If you wanted that to affect the $PATH variable of the shell that invokes that script, you'd need that shell to source (as with the . or source command) that deactivate script, or make deactivate a function of that shell.



    Or alternatively, you can have the deactivate script output the shell code that would be needed to remove those entries from $PATH (for instance by adding export -p PATH after having modified it) and make sure to invoke it as:



    eval "$(deactivate)"


    Within the shell where you want that entry to be removed from $PATH.



    Something like:



    #! /bin/zsh -
    path=("${path[@]:#/usr/local/cuda-9.0/bin}")
    export -p PATH





    share|improve this answer






























      0














      export PATH=$(echo ${PATH} | sed -r 's|/usr/local/cuda-9.0/bin||')


      Or its more correct version:



      export PATH="$(printf '%sn' "$PATH" | sed 's|/usr/local/cuda-9.0/bin||')"


      Updates the content of the $PATH variable of the shell invoking that command by removing the first occurrence of /usr/local/cuda-9.0/bin in it. For instance, it would change a /bin:/opt/usr/local/cuda-9.0/bin/v2:/usr/local/cuda-9.0/bin to /bin:/opt/v2:/usr/local/cuda-9.0/bin which is not what you want.



      To remove full elements, you could do:



      export PATH="$(
      printf ':%s:n' "$PATH" |
      sed '
      :1
      s|:/usr/local/cuda-9.0/bin:|:|g; t1
      s|^:||; s|:$|'
      )"


      Or with zsh:



      path=("${path[@]:#/usr/local/cuda-9.0/bin}")


      Note that if $PATH initially only contained /usr/local/cuda-9.0/bin, then the end result will be an empty $PATH. And empty $PATH means search for executables in the current directory which is not what you want. If that is a possibility, then you may want to make $PATH something like /inexistent/to/disable/PATH/lookup.



      Note that in any case, it only changes the $PATH variable of the shell that runs that code. If you wanted that to affect the $PATH variable of the shell that invokes that script, you'd need that shell to source (as with the . or source command) that deactivate script, or make deactivate a function of that shell.



      Or alternatively, you can have the deactivate script output the shell code that would be needed to remove those entries from $PATH (for instance by adding export -p PATH after having modified it) and make sure to invoke it as:



      eval "$(deactivate)"


      Within the shell where you want that entry to be removed from $PATH.



      Something like:



      #! /bin/zsh -
      path=("${path[@]:#/usr/local/cuda-9.0/bin}")
      export -p PATH





      share|improve this answer




























        0












        0








        0







        export PATH=$(echo ${PATH} | sed -r 's|/usr/local/cuda-9.0/bin||')


        Or its more correct version:



        export PATH="$(printf '%sn' "$PATH" | sed 's|/usr/local/cuda-9.0/bin||')"


        Updates the content of the $PATH variable of the shell invoking that command by removing the first occurrence of /usr/local/cuda-9.0/bin in it. For instance, it would change a /bin:/opt/usr/local/cuda-9.0/bin/v2:/usr/local/cuda-9.0/bin to /bin:/opt/v2:/usr/local/cuda-9.0/bin which is not what you want.



        To remove full elements, you could do:



        export PATH="$(
        printf ':%s:n' "$PATH" |
        sed '
        :1
        s|:/usr/local/cuda-9.0/bin:|:|g; t1
        s|^:||; s|:$|'
        )"


        Or with zsh:



        path=("${path[@]:#/usr/local/cuda-9.0/bin}")


        Note that if $PATH initially only contained /usr/local/cuda-9.0/bin, then the end result will be an empty $PATH. And empty $PATH means search for executables in the current directory which is not what you want. If that is a possibility, then you may want to make $PATH something like /inexistent/to/disable/PATH/lookup.



        Note that in any case, it only changes the $PATH variable of the shell that runs that code. If you wanted that to affect the $PATH variable of the shell that invokes that script, you'd need that shell to source (as with the . or source command) that deactivate script, or make deactivate a function of that shell.



        Or alternatively, you can have the deactivate script output the shell code that would be needed to remove those entries from $PATH (for instance by adding export -p PATH after having modified it) and make sure to invoke it as:



        eval "$(deactivate)"


        Within the shell where you want that entry to be removed from $PATH.



        Something like:



        #! /bin/zsh -
        path=("${path[@]:#/usr/local/cuda-9.0/bin}")
        export -p PATH





        share|improve this answer















        export PATH=$(echo ${PATH} | sed -r 's|/usr/local/cuda-9.0/bin||')


        Or its more correct version:



        export PATH="$(printf '%sn' "$PATH" | sed 's|/usr/local/cuda-9.0/bin||')"


        Updates the content of the $PATH variable of the shell invoking that command by removing the first occurrence of /usr/local/cuda-9.0/bin in it. For instance, it would change a /bin:/opt/usr/local/cuda-9.0/bin/v2:/usr/local/cuda-9.0/bin to /bin:/opt/v2:/usr/local/cuda-9.0/bin which is not what you want.



        To remove full elements, you could do:



        export PATH="$(
        printf ':%s:n' "$PATH" |
        sed '
        :1
        s|:/usr/local/cuda-9.0/bin:|:|g; t1
        s|^:||; s|:$|'
        )"


        Or with zsh:



        path=("${path[@]:#/usr/local/cuda-9.0/bin}")


        Note that if $PATH initially only contained /usr/local/cuda-9.0/bin, then the end result will be an empty $PATH. And empty $PATH means search for executables in the current directory which is not what you want. If that is a possibility, then you may want to make $PATH something like /inexistent/to/disable/PATH/lookup.



        Note that in any case, it only changes the $PATH variable of the shell that runs that code. If you wanted that to affect the $PATH variable of the shell that invokes that script, you'd need that shell to source (as with the . or source command) that deactivate script, or make deactivate a function of that shell.



        Or alternatively, you can have the deactivate script output the shell code that would be needed to remove those entries from $PATH (for instance by adding export -p PATH after having modified it) and make sure to invoke it as:



        eval "$(deactivate)"


        Within the shell where you want that entry to be removed from $PATH.



        Something like:



        #! /bin/zsh -
        path=("${path[@]:#/usr/local/cuda-9.0/bin}")
        export -p PATH






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 3 at 11:57

























        answered Jan 3 at 11:50









        Stéphane ChazelasStéphane Chazelas

        301k55564916




        301k55564916















            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre