Fandom

Scratchpad

BASH Programming Basics

215,877pages on
this wiki
Add New Page
Discuss this page0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Welcome to the BASH Programming Basics mini wiki at Scratchpad!

You can use the box below to create new pages for this mini-wiki. Make sure you type [[Category:BASH Programming Basics]] on the page before you save it to make it part of the BASH Programming Basics wiki (preload can be enabled to automate this task, by clicking this link and saving that page. Afterwards, you may need to purge this page, if you still see this message).

BASH Shell Programming Basics

Introduction

BASH is a type of UNIX shell which stands for Bourne Again SHell, named after it's author.

  • UNIX is a free Operating System which as a basis to a Linux Distribution is becoming popular lately.
  • A shell is the command line interface which allows for user input to control programs which operate various functions of the computer.

Why Would I want to program in BASH?

  1. BASH is available on most installations of Unix and Linux
  2. BASH is free to use
  3. BASH can be used to control and provide input and output of Linux Applications through the use of scripts

Basic Script Creation

  • This guide assumes you are familiar with some aspects of text editors and Linux or UNIX. A text editor like Emacs, Vi, or Pico is used to create an ASCII text file.

Helloworld1

  • Scripts which use BASH must have Linux execute permissions.
    • The easiest way to make sure you have execute permissions is to create a blank text file using vi and then issuing the command chmod 700 myscript , with myscript being the name of the file you created.

After Initial Creation

Steps to make a BASH Script

  • The BASH script needs the line #!/usr/bin/bash or its equivalent.
    • If the path to BASH on your machine is different you can find it by using the command which bash
  • You can use the number sign (#) to add a comment to make your program readable.
    • Eg. #This is a comment, the program will not read this line
  • Create a variable by directly assigning it
    • mytext="hello world"
    • mynumber=27
  • Use echo or another built in command to create output
    • echo displays on the screen what follows it.
    • To use a variable, put the $ in front of it to use its value once it has already been assigned a value.
      • echo "$mytext" will display "hello world" to the user
      • echo "$mynumber is my favorite number" will display "27 is my favorite number"
      • echo "mynumber" will display only "mynumber"

Your first "Hello World" BASH program

Using the above techniques you are now able to create your first BASH script.

Putting together the examples above you have:
#!/usr/bin/bash
#This is a comment
mytext="hello world"
echo "$mytext"

Helloworld2

Use ./program_name to execute the program
Its output is "hello world"

Helloworld3


Reference

Also on Fandom

Random wikia