How to Create a Script in Linux?
Contents

Create a simple Git deployment script.

  • Create a bin directory. The first step is to create a bin directory.
  • Export your bin directory to the PATH. Open .bash_profile , which will be located at /Users/tania/.bash_profile , and add this line to the file.
  • Create a script file and make it executable.

Home

  • Use a text editor such as vi.
  • Save and close the file (exit from vi).
  • Make the script executable.
  • You should then of course test the script, and once satisfied with the output, move it to the production environment.
  • The simplest program in Bash consists of a line that tells the computer a command.

How to Write a Shell Script Using Bash Shell in Ubuntu

  • Launch the Terminal.
  • In the Terminal launch the vi/vim editor.
  • In the terminal window, type vim ListDir.sh and hit enter.
  • At the top type the following code #! /bin/bash.
  • Type the code as shown in the figure.
  • Type the following key combinations, Esc + : + wq to escape the editor.

Follow these steps:

  • Run nano hello.sh.
  • nano should open up and present an empty file for you to work in.
  • Then press Ctrl-X on your keyboard to Exit nano.
  • nano will ask you if you want to save the modified file.
  • nano will then confirm if you want to save to the file named hello.sh .

Linux (advanced)[edit]

  • save your hello.py program in the ~/pythonpractice folder.
  • Open up the terminal program.
  • Type cd ~/pythonpractice to change directory to your pythonpractice folder, and hit Enter.
  • Type chmod a+x hello.py to tell Linux that it is an executable program.
  • Type ./hello.py to run your program!

How do I make a script executable in Linux?

These are some of the pre-requisites of using directly the script name:

  1. Add the she-bang {#!/bin/bash) line at the very top.
  2. Using chmod u+x scriptname make the script executable. (where scriptname is the name of your script)
  3. Place the script under /usr/local/bin folder.
  4. Run the script using just the name of the script.

How do I write a bash script?

To create a bash script, you place #!/bin/bash at the top of the file. To execute the script from the current directory, you can run ./scriptname and pass any parameters you wish. When the shell executes a script, it finds the #!/path/to/interpreter .

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest