4 Ways of Executing a Shell Script in UNIX / Linux

9 篇文章 0 订阅
8 篇文章 0 订阅

There are four ways to execute a shell script. Each way has it’s own meaning as explained in this article.

1. Execute Shell Script Using File Name

Use the shell script file name to execute it either by using it’s relative path or absolute path as shown below.

$ cd /home/sathiya
$ ./scriptfile

(or)
$ /home/sathiya/scriptfile

If you have the shebang, then it will be executed using the command interpreter specified in the shebang. If you are beginner in shell scripting, refer our earlier article Shell Script Execution Guidelines for Newbies

2. Execute Shell SCript by Specifying the Interpreter

You can also execute a unix shell script by specifying the interpreter in the command line as shown below.

Execute using sh interpreter
$ sh scriptfile
Execute using bash interpreter
$ bash scriptfile

Irrespective of what is being used as shebang, the interpreter which you have specified will be used for execution. You can use any interpreter (sh, ksh, bash, csh etc.,).

3. Execute Shell Script Using .  ./ (dot space dot slash)

only . (dot) means execute given command file in current shell without starting the new copy of shell

While executing the shell script using “dot space dot slash”, as shown below, it will execute the script in the current shell without forking a sub shell.

$ . ./scriptfile

In other words, this executes the commands specified in the script file in the current shell, and prepares the environment for you.

“dot space dot slash” Usage Example:

Typically we use this method, anytime we change something in the .bashrc or  .bash_profile. i.e After changing the .bashrc or .bash_profile we can either logout and login for the changes to takeplace (or) use “dot space dot slash” to execute .bashrc or .bash_profile for the changes to take effect without logout and login.

$ cd ~

$ . ./.bashrc

$ . ./.bash_profile

4. Execute Shell Script Using Source Command

The builtin source command is synonym for the . (dot) explained above. If you are not comfortable with the “dot space dot slash”method, then you can use source command as shown below, as both are same.

$ source ~/.bashrc

Which method do you prefer to execute a shell script?


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值