Bash script auto deleting

Hello everyone,
I have started to learn development. As part of this I have now wanted to create my own bash script to automate docker. (I got tired of writing the same commands at start up just to spin up the docker container)
So I started to look how to do this.
I found that I create a script and then place it in /usr/local/bin/ to make the script exectuable without having to type any other commands

I created a directory inside /usr/local/bin/bash-scripts/
inside this dir i created my bash.sh file

In the terminal if i cd to /usr/local/bin/bash-scripts/ and use sh bash.sh the script will run

As soon as I cd out to ~ and then run sh bash.sh I am told the file doesn’t exist and sure enough
when I cd back the file location it has been removed

am I doing something wrong or is this a bug?

Many thanks

I don’t develop or do much coding but when I find it advantageous to use scripts, I write them in a local directory called scripts. Within this directory I place all .sh items and they seem to work without disappearing.

Try this and see if it works for you. And make sure your permissions are set so that your scripts function as intended.

Good luck archaic.

Edit: I’d follow Bill’s advice rather than mine. I’ve seen it before and have a ~/bin on another install.

Hi @ArchaicLord, Perhaps a more modern developer can add much more but here’s what I’ve seen for years - putting personal scripts in ~/bin/ .

There’s a reason for that and it continues to this day in Ubuntu. When logging in, the default scripts that run looks to see if ~/bin exists and, if it does, places the appropriate /home/name/bin location in the environment variable known as $PATH. Take a peek:

echo $PATH

You should find /usr/local/bin already there, too, which is the reason your script works there. When you type a command by itself the shell searches $PATH and the first one it finds runs.

There’s many ways you can go. You can put all the scripts in /usr/local/bin and they should just work. This also has the advantage of any user able to access compared to ~/bin in your personal home directory.

Why a script would disappear or get moved is baffling. I’ve never heard of this (anyone?). The key is /usr/local/bin is already in $PATH but /usr/local/bin/scripts is not.

Hope this helps, the topic runs deep. :slight_smile:

1 Like

Thank you @Bill_MI and @mdooley for your replies.

I couldn’t get /usr/local/bin to work.

I created ~/bin and I know for now I can call any script in there by using
sh ~/bin/script.sh

This is working for now :slight_smile:

It should work by just using:

script.sh

If it does not work, it is likely because the execute (x) permission is not set for the file. It can be set with this:

chmod +x ~/bin/script.sh

The biggest difference regarding /usr/local/bin is only root can create a file there since it is a system area. The x permission has the same need.

There’s a lot of ways to do the same thing like using sh forces the sh shell and doesn’t need +x. I’m mentioning the more classical ways and hope it helps.

1 Like

I have been reading and experimenting.

I have been coming to the conclusion that placing scripts in /usr/local/bin is not a good idea for the reason that if I ever want to change machines, backup re-install the desktop then if I keep it all in my home directory I can simply then zip it all up and know I have everything without having to make sure I don’t things in other places. (except for programs whiich live in /opt/ i believe)

1 Like