How To Write Bash Scripts In Linux - Complete Guide (Part 10 - Storing Scripts)

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/bin directory is highlighted as an ideal location for storing user-created scripts, making them accessible and organized.
  • Moving the update.sh script into /usr/local/bin requires administrative privileges, using the mv command.

Managing Permissions and Extensions

  • Changing ownership of the script to root enhances security by restricting modification rights to authorized users only.
  • The .sh extension 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 which command 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/bin is 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.
Video description

By now, you’ve probably created a variety of Bash scripts—but where should you store them? In this episode of the LearnLinuxTV Bash scripting course, you’ll learn the best practices for organizing and storing your scripts within the Linux filesystem. Discover the standard directory structure, where to place your scripts for system-wide or user-specific use, and how proper script organization improves manageability and execution. ━━━━━━━━━━━━━━━━━━━━━━ 🕐 Time Codes 00:00 - Intro 00:48 - The weaknesses of how we've been running scripts so far 02:39 - Where should you store your Bash Scripts? (and the FHS) 04:20 - Moving a script into /usr/local/bin 06:24 - Why it's not necessary to use file extensions in Linux filenames 08:08 - Understanding $PATH on the Linux shell 10:48 - Adding a directory to your $PATH variable in Linux OTHER BASH SCRIPTING SERIES EPISODES - Class 01 ➜ Introduction: https://linux.video/bash1 - Class 02 ➜ Hello World: https://linux.video/bash2 - Class 03 ➜ Variables: https://linux.video/bash3 - Class 04 ➜ Math Functions: https://linux.video/bash4 - Class 05 ➜ If Statements: https://linux.video/bash5 - Class 06 ➜ Exit Codes: https://linux.video/bash6 - Class 07 ➜ While Loops: https://linux.video/bash7 - Class 08 ➜ Universal Update Script: https://linux.video/bash8 - Class 09 ➜ For Loops: https://linux.video/bash9 - Class 10 ➜ Where To Store Your Scripts: https://linux.video/bash10 - Class 11 ➜ Data Streams: https://linux.video/bash11 - Class 12 ➜ Functions: https://linux.video/bash12 - Class 13 ➜ Case Statements: https://linux.video/bash13 - Class 14 ➜ Scheduling Jobs (Part 1): https://linux.video/bash14 - Class 15 ➜ Scheduling Jobs (Part 2): https://linux.video/bash15 - Class 16 ➜ Arguments: https://linux.video/bash16 - Class 17 ➜ Building a Backup Script: https://linux.video/bash17 - Class 18 ➜ Closing: https://linux.video/bash18━━━━━━━━━━━━━━━━━━━━━━ *🎓 Learn Linux TV* Your source for Linux-related Fun and Learning! https://www.learnlinux.tv https://community.learnlinux.tv https://learnlinux.link/jay ━━━━━━━━━━━━━━━━━━━━━━ *🤝 Affiliate Notice* Some of the links mentioned in this description are affiliate links. If you purchase through them, it helps keep the content flowing. https://www.learnlinux.tv/content-ethics ━━━━━━━━━━━━━━━━━━━━━━ *⚠️ Use Content Responsibly* Learn Linux TV shares technical content intended to teach and help you, but it comes with no warranty. The channel is not liable for any damages from its use. Always ensure you have proper permissions, follow company policies, and comply with all applicable laws while working with technology. #linuxforbeginners #commandline #technology