4 Ways of Executing a Shell Script in UNIX / Linux
2014-03-20 20:00
651 查看
There are four ways to execute a shell script. Each way has it’s own meaning as explained in this article.
(or)
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
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.,).
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.
In other words, this executes the commands specified in the script file in the current shell, and prepares the environment for you.
for the changes to take effect without logout and login.
Which method do you prefer to execute a shell script?
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 shellWhile 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_profilefor 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?
相关文章推荐
- 10 篇对初学者和专家都有用的 Linux 命令教程
- Linux 与 Windows 对UNICODE 的处理方式
- Ubuntu12.04下QQ完美走起啊!走起啊!有木有啊!
- 解決Linux下Android开发真机调试设备不被识别问题
- 运维入门
- 运维提升
- Linux 自检和 SystemTap
- 动态清空 nohup 输出文件
- install scrapy with pip and easy_install
- Ubuntu Linux使用体验
- c语言实现hashmap(转载)
- Linux 信号signal处理机制
- linux下mysql添加用户
- Scientific Linux 5.5 图形安装教程
- 基于 Linux 集群环境上 GPFS 的问题诊断
- 谁是桌面王者?Win PK Linux三大镇山之宝
- vivi下重新调整分区
- Linux VS Unix:Linux欲一统天下 Unix不死