3

I'm learning how to create shell scripts on UNIX for a college class. The textbook says I should just be able to type the filename in my terminal and it should run, but so far I haven't been able to get that to work. But it works fine when I type in ./myscript. Currently, I am typing the scripts out on my Mac terminal, but I had FreeBSD loaded onto a virtual box and it was doing the same thing to me.

I think maybe I need to change my environment path? I remember I had to do that when I started writing python files.

  • FreeBSD and macOS are not Ubuntu. Please ask on https://unix.stackexchange.com/ instead. – wjandrea Aug 27 '18 at 21:50
  • 1
    Use the full path or put it in ~/bin – Panther Aug 27 '18 at 22:22
  • In the past people had . in their PATH which means, at some point, if not found elsewhere, the search will be in current directory. Especially for root or any kind of high power user this is a security nightmare about to happen, so it is best not to have it and force users to input the full path name. – Patrick Mevzek Aug 31 '18 at 02:00

1 Answers1

1

To run scripts you have written yourself you have a couple options.

OPTION 1

You can store them in /usr/bin.

This is personally what I do as I read somewhere a long time ago that /usr/bin was for things exactly like this and I've never really thought of it otherwise

I do however keep a copy of the scripts in a separate folder Documents/SourceCode/ which is where I do all my editing and testing. then I just copy the script over to /usr/bin

OPTION 2

You can just store them all in your testing folder so in my case...

Documents/SourceCode/

and add this to my path variable.

export PATH=$(PATH):/home/user/Documents/SourceCode

j-money
  • 2,382
  • 4
    Putting the script in ~/bin is better than either of those options. Or if for multiple users /usr/local/bin – Panther Aug 27 '18 at 22:24
  • @Panther Arguably it's the preferred method, but option 2 is same as ~/bin. Difference is that .bashrc on Ubuntu already adds ~/bin to PATH if it exists. I agree with your comment but only partially. – Sergiy Kolodyazhnyy Aug 27 '18 at 22:45
  • @Panther thanks for the catch! I am curious though as to what makes ~/bin "better"? – j-money Aug 28 '18 at 03:28
  • With ubuntu, ~/bin is on a users path by default and it does not require root powers so it is very simple – Panther Aug 28 '18 at 03:38
  • Ah this would explain, I've never had a multi user system so I've never had to worry about anyone else :'D – j-money Aug 28 '18 at 03:46