Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Whats the difference between running a shell scrip

scmuser created the topic: Whats the difference between running a shell scrip
Whats the difference between running a shell script as ./script.sh and sh script.sh

have a script that looks like this

#!/bin/bash

function something() {
echo “hello world!!”
}

something | tee logfile

I have set the execute permission on this file and when I try running the file like this

$./script.sh

it runs perfectly fine, but when I run it on the command line like this

$sh script.sh

It throws up an error. Why does this happen and what are the ways in which I can fix this.

scmuser replied the topic: Re:Whats the difference between running a shell scrip
Running it as ./script.sh will make the kernel read the first line (the shebang), and then invoke bash to interpret the script. Running it as sh script.sh uses whatever shell your system defaults sh to (on Ubuntu this is Dash, which is sh-compatible, but doesn’t support some of the extra features of Bash).

You can fix it by invoking it as bash script.sh, or if it’s your machine you can change /bin/sh to be bash and not whatever it is currently (usually just by symlinking it – rm /bin/sh && ln -s /bin/bash /bin/sh). Or you can just use ./script.sh instead if that’s already working 😉

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x