bash command `cd /usr/local/src` (including backticks) has no effect

后端 未结 1 977
不知归路
不知归路 2021-01-27 07:08

I am fairly new to bash, so I don\'t know where this problem is appearing. But, I have this script, which is supposed to just change cd, download a tar file and extract it. I

相关标签:
1条回答
  • 2021-01-27 07:41

    As edited, your script now says that you're running

    #!/bin/bash
    `cd /usr/local/src`
    `wget http://nginx.org/download/nginx-1.6.2.tar.gz -O nginx-1.6.2.tar.gz`
    `tar -xzf  nginx-1.6.2.tar.gz`
    

    The backticks cause each command to be run in a subshell; and each subshell's output to be read, string-split, and run as a second command.

    Also, because the cd is in a subshell, its effects don't carry over to the main shell.


    So: Remove the backticks (and make sure your script exits if the cd fails; I do this here with the -e flag). You should only be running:

    #!/bin/bash -e
    cd /usr/local/src
    wget http://nginx.org/download/nginx-1.6.2.tar.gz -O nginx-1.6.2.tar.gz
    tar -xzf  nginx-1.6.2.tar.gz
    

    ...which is to say: Your commands must not start and end with ` characters.

    0 讨论(0)
提交回复
热议问题