Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#!/bin/bash
if [ `jobs|wc -l` = 0 ]
then
echo 'There are no jobs'
else
echo 'There are some jobs'
fi
When I execute it, it always tells me 'There are no jobs', even if there are stopped jobs in the shell. Running "jobs|wc -l" directly at the CLI works as expected.
If you edit your file (remove the hash-bang) and make it like this:
Code:
if [ `jobs|wc -l` = 0 ]
then
echo 'There are no jobs'
else
echo 'There are some jobs'
fi
And parse it:
Code:
$ ls -l example
-rw-r----- 1 druuna internet 137 May 2 16:07 example
$ . example
There are no jobs
As you can see the file doesn't even need execute permissions. Do mind the space between the dot and the filename.
The major difference between parsing and executing a file is the way the shell is used. Parsing is done in the shell you are in, while executing will start a new shell and runs the program in the new shell.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Quote:
Originally Posted by SlowCoder
If I remove the #!/bin/bash from my code, will that allow it to run in the current shell?
No, the prefix is unrelated, it's the way you launch the script that matters.
Quote:
I tried it with it and without, with same results.
Indeed, the #! prefix is only used when the script is executed by the O/S as an executable program to tell what command interpreter to use.
When you run it with the shell "." command, the whole first line is ignored as it starts with "#" which is the comment prefix.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.