Main Menu

search

You are here

Linux: Script Files

[last updated: 2025-03-28]
Linux home page
Linux script programming
-----

Disclaimer as usual: This is my current best guess as to what's going on...

  • General Notes:
    • Script files end with .sh extension
    • Create script files with any text editor
      You must make the text file executable.
    • The first line of the script file must be:
      #!/bin/bash
    • Script files contain linux commands
    • Script files can contain lines to execute eg. Python programs:

    -------------------------------------------

  • Create and execute a script file:
    • make a file named test02.sh,
      that contains three lines:
         #!/bin/bash
         pwd
         ls
    • make the file executable:
      $ sudo chmod +x test02.sh
      -or- in File Manager:
        right-click the file
        select Properties (at bottom of drop-down) > Permissions
        click "Allow executing file as program"

    • execute script files with:
      $ [path-to-directory-where-file-is-located] /test02.sh


      or, if you are currently in the directory where the file is located,
            -or- if you set the PATH to include the directory where your script files are located
      then execute it with:
      $ ./test02.sh

    --------------------------------------------------------------------------------

  • Change directory using a script file, and make the new directory persist after the command is done:
    • Add exec bash to the end of your file:
        #!/bin/bash
        cd "/home/jay/Documents"
        exec bash

    ---------------------------------------------------------------------

  • Make a clickable desktop link:
    • [from Reddit]imo the simplest answer is -
      Check that the .sh file works if clicked or run from its own directory.
      If it works there, right-click on it in Files aka Nautilus, select 'Make Link'
      This produces a file named 'link to xxxx.sh' in the same directory
      Drag this onto the desktop
      Rename it how you wish eg. clean off 'link to' and 'sh', just leave the name, it will still work.

    • [from some other forum...]
      I think it would be better to use a launcher file for your script by creating a ~/Desktop/ssh_home.desktop file with the following contents:
        [Desktop Entry]
        Version=1.0
        Exec=/home/yourname/bin/ssh_home.sh
        Name=SSH Server
        GenericName=SSH Server
        Comment=Connect to My Server
        Encoding=UTF-8
        Terminal=true
        Type=Application
        Categories=Application;Network;

      That way you will have a clickable icon which will launch your script.
      You may have to also set the executable flag with chmod:

        chmod +x ~/Desktop/ssh_home.desktop

    ---------------------------------------------------------------------

  • see [...here...] for using script files to edit metadata on audio/music files ...
    -------------------------------------------------

  • See also:
    run script file at startup on rPi
  • -------------------------------------------------

.

.

.

eof