executable bash script

All Gurus once were Newbies
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read how to get help
Post Reply
itlarson
Level 2
Level 2
Posts: 56
Joined: Sat Jan 09, 2010 5:44 pm

executable bash script

Post by itlarson »

I have been trying to make a bash script executable, but it isn't working like it should. Here is my test script:
#!/bin/bash
echo "bash script test"

This is saved in my home directory.
To make it executable I run:
chmod uga+x test.sh

if I run:
~ $ bash test.sh

I get the output:
bash script test

but if I run:
$ test.sh

I get:
test.sh: command not found

What am I missing? My understanding is that this should work.
HANAX
Level 1
Level 1
Posts: 5
Joined: Sun Jan 10, 2010 7:43 am

Re: executable bash script

Post by HANAX »

try

Code: Select all

./test
notice "./' - it means active directory
Last edited by HANAX on Sun Jan 17, 2010 5:42 am, edited 1 time in total.
HANAX
itlarson
Level 2
Level 2
Posts: 56
Joined: Sat Jan 09, 2010 5:44 pm

Re: executable bash script

Post by itlarson »

./test.sh
gives the desired output. Why do I have to prepend the "./"? you don't need to do this for most commands.
itlarson
Level 2
Level 2
Posts: 56
Joined: Sat Jan 09, 2010 5:44 pm

Re: executable bash script

Post by itlarson »

Another issue with this is it won't work if you aren't in the same directory. I would like to run scripts in my $PATH as if they are regular commands.
User avatar
Kaye
Level 5
Level 5
Posts: 933
Joined: Fri Feb 06, 2009 5:05 pm
Location: Boston College
Contact:

Re: executable bash script

Post by Kaye »

Then you either need to add them to your path, or move them to /usr/bin/ or another directory that's already in your path. Just because a script is executable doesn't mean that your system knows about it no matter where you are.

Edit: If you decide you want to add them to your path, you'll need to edit your .bashrc file, just to point you in the right direction.
"In somnis veritas"
Antivirus or defragging?
Image
emorrp1
Level 8
Level 8
Posts: 2158
Joined: Thu Feb 26, 2009 8:58 pm

Re: executable bash script

Post by emorrp1 »

commands are located by the PATH environment variable. You could add "." to your path if you wanted to run commands from the current directory without the ./ but this could cause trouble. I recommend creating a ~/bin directory, adding that to your path and putting any custom scripts you create for general use in there.
If you have a question that has been answered and solved, then please edit your original post and put a [SOLVED] at the end of your subject header
Hint - use a google search including the search term site:forums.linuxmint.com
itlarson
Level 2
Level 2
Posts: 56
Joined: Sat Jan 09, 2010 5:44 pm

Re: executable bash script

Post by itlarson »

I already had added the directory to the path. Strangely after rebooting for an unrelated reason, it has started working:
~ $ test.sh
bash script test

Mission accomplished, I guess. Still don't know why this didn't work immediately.
User avatar
Kaye
Level 5
Level 5
Posts: 933
Joined: Fri Feb 06, 2009 5:05 pm
Location: Boston College
Contact:

Re: executable bash script

Post by Kaye »

Were you trying to use a terminal that had been opened before the path change was made? That would do it.
"In somnis veritas"
Antivirus or defragging?
Image
itlarson
Level 2
Level 2
Posts: 56
Joined: Sat Jan 09, 2010 5:44 pm

Re: executable bash script

Post by itlarson »

That was almost certainly it!
Post Reply

Return to “Newbie Questions”