Difference between “./” and “sh” in UNIX

前端 未结 4 1097
鱼传尺愫
鱼传尺愫 2021-01-30 18:07

Sometimes i see that few scripts are executed through \"sh\" command and sometimes through \"./\" command.I am not able to understand the exact difference between them.Please he

4条回答
  •  無奈伤痛
    2021-01-30 18:35

    With sh , we can run a script that doesn’t have execute permission set on it, we run it as argument for sh, but ./ needs the permission as it is supposed to be an executable. In both cases, new shell will be created to run the script. See the below example:

    root@ub18:~/shell# vi test1.sh
    #!/bin/bash
    my_var=hello
    echo $my_var
    #Shows the current shell processid
    echo $$
    root@ub18:~/shell# echo $$
    1896
    root@ub18:~/shell# sh test1.sh
    hello
    2093
    root@ub18:~/shell# ./test1.sh
    -su: ./test1.sh: Permission denied
    root@ub18:~/shell# chmod +x ./test1.sh
    root@ub18:~/shell# ./test1.sh
    hello
    2102
    root@ub18:~/shell# ./test1.sh
    hello
    2103
    root@ub18:~/shell# ./test1.sh
    hello
    2104
    root@ub18:~/shell# sh test1.sh
    hello
    2106
    

提交回复
热议问题