This post is to answer a question that someone asked about automating a task with Bash. It’s just how I would do it, and I’m not an expert shell scripter, so if there is a better way, please comment below.
I put all my projects in a directory called ~/code
. If my script were called my_command
, I would store it in a file at ~/code/my_command/my_command
(no file extension).
The my_command
file would contain these contents:
#!/bin/bash
# write your script commands here
After the commands are entered, you can make the script executable by typing this in the same directory as the my_command
file:
$ chmod u+x ./my_command
I made a directory on my computer called ~/bin
which stores executable files. To link my project (which is in ~/code/some_project_name
) to the ~/bin
directory, I create a symlink:
$ ln -s ~/code/my_command/my_command ~/bin/my_command
In my terminal’s config file where I set my PATH
variable, I add ~/bin
to the PATH
.
PATH="$PATH:$HOME/bin"
After that is done, you should be able to type this anywhere in the terminal to run your script’s commands:
$ my_command