How To Write Bash Scripts In Linux - Complete Guide (Part 10 - Storing Scripts)
Where to Store Scripts on Linux?
Introduction to Script Storage
- The speaker discusses the importance of storing scripts effectively in the Linux file system rather than relying on flash drives for backup.
- Focus is placed on identifying appropriate directories within the Linux file system for script storage.
Example Script: Universal Update Script
- The speaker references a previously created universal update script from lesson 8, renamed as
update.sh, to illustrate practical usage.
- Running scripts from the home directory is discouraged due to accessibility issues for other users who may need to execute them.
Importance of Centralized Script Location
- A centralized location for scripts ensures that co-workers or administrators can access and run necessary scripts when needed.
- Introduction of the File System Hierarchy Standard (FHS), which outlines standard directory purposes across Linux distributions.
Recommended Directory: /usr/local/bin
- The
/usr/local/bindirectory is highlighted as an ideal location for storing user-created scripts, making them accessible and organized.
- Moving the
update.shscript into/usr/local/binrequires administrative privileges, using themvcommand.
Managing Permissions and Extensions
- Changing ownership of the script to root enhances security by restricting modification rights to authorized users only.
- The
.shextension is not mandatory in Linux; scripts can be executed without it if they contain a shebang line indicating their interpreter.
Executing Scripts Without Full Path
- By placing the script in
/usr/local/bin, it can be executed simply by typing its name (update) without needing a full path reference.
- The
whichcommand confirms that the shell recognizes where the executable resides, demonstrating effective path management.
Understanding PATH Variable
- Explanation of how the shell uses a special variable called
PATH, which contains directories searched for executable files.
- Users can view their current environment variables with commands like
env, confirming that/usr/local/binis included in their PATH.
Modifying PATH Variable (if necessary)
- Instructions are provided on how to add new directories to the PATH variable using export commands if required, although this should rarely be necessary for standard setups.
Conclusion and Key Takeaways
- Emphasis on understanding where to store scripts effectively within Linux systems, particularly recommending
/usr/local/bin.
- Encouragement given regarding learning outcomes from this lesson and anticipation of future lessons.