background preloader

Bash shell

Facebook Twitter

Bash Special Parameters Explained with 4 Example Shell Scripts. As part of our on-going bash tutorial series, we discussed about bash positional parameters in our previous article.

Bash Special Parameters Explained with 4 Example Shell Scripts

In this article let us discuss about the bash special parameters with few practical shell script examples. Some of the bash special parameters that we will discuss in this article are: $*, $@, $#, $$, $! , $? , $-, $_ To access the whole list of positional parameters, the two special parameters $* and $@ are available. Within double quotes, however, they differ: $* within a pair of double quotes is equivalent to the list of positional parameters, separated by the first character of IFS “$1c$2c$3…”. $@ within a pair of double quotes is equivalent to the list of positional parameters, separated by unquoted spaces, i.e., “$1″ “$2″..” Example 1: Use Bash $* and $@ to Expand Positional Parameters This example shows the value available in $* and $@. First, create the expan.sh as shown below. Manipulating Strings. Extracts substring from $string at $position.

Manipulating Strings

If the $string parameter is "*" or "@", then this extracts the positional parameters, starting at $position. Extracts $length characters of substring from $string at $position. The position and length arguments can be "parameterized," that is, represented as a variable, rather than as a numerical constant. Example 10-2. Generating an 8-character "random" string. BASH Programming - Introduction HOW-TO. By Mike G mikkey at dynamo.com.ar Thu Jul 27 09:36:18 ART 2000 This article intends to help you to start programming basic-intermediate shell scripts.

BASH Programming - Introduction HOW-TO

It does not intend to be an advanced document (see the title). I am NOT an expert nor guru shell programmer. I decided to write this because I'll learn a lot and it might be useful to other people. Bash display dialog boxes - Linux Shell Scripting Tutorial - A Beginner's handbook. The dialog command allows you to display a variety of questions or display messages using dialog boxes from a shell script.

Bash display dialog boxes - Linux Shell Scripting Tutorial - A Beginner's handbook

Use the dialog utility for creating TTY (terminal) dialog boxes. Install dialog command Type the following command to install the dialog command line utility under Debian or Ubuntu Linux: sudo apt-get updatesudo apt-get install dialog Type the following command to install the dialog command line utility under CentOS or Redhat Linux: Syntax The syntax is as follows: dialog --common-options --boxType "Text" Height Width --box-specific-option --common-options are used to set dialog boxes background color, title, etc.

Your first dialog Type the following command at a shell prompt: dialog --title "Hello" --msgbox 'Hello world! ' A message box is displayed on the screen with a single OK button. Understanding dialog options --title "Hello" : Set a "title string" (caption) to be displayed at the top of the dialog box. Setting backtitle Positioning the box Where, Bash String Manipulation Examples – Length, Substring, Find and Replace. In bash shell, when you use a dollar sign followed by a variable name, shell expands the variable with its value.

Bash String Manipulation Examples – Length, Substring, Find and Replace

This feature of shell is called parameter expansion. But parameter expansion has numerous other forms which allow you to expand a parameter and modify the value or substitute other values in the expansion process. In this article, let us review how to use the parameter expansion concept for string manipulation operations. This article is part of the on-going bash tutorial series. Refer to our earlier article on bash { } expansion. Color Bash Prompt. There are a variety of possibilities for Bash's prompt (PS1), and customizing it can help you be more productive at the command line.

Color Bash Prompt

You can add additional information to your prompt, or you can simply add color to it to make the prompt stand out. Applying changes To apply changes from this article to your .bashrc (without ending subshells), do: $ source ~/.bashrc Basic prompts The following settings are useful for distinguishing the root prompt from non-root users. Regular user A green prompt for regular users: [chiri@zetsubou ~]$ _ ~/.bashrc #PS1='[\u@\h \W]\$ ' # Default PS1='\[\e[1;32m\][\u@\h \W]\$\[\e[0m\] ' Root A red prompt for root (copy from /etc/skel/, if not already): [root@zetsubou ~]# _ /root/.bashrc. Bash Guide for Beginners. Variables. 3.2.1.

Variables

Types of variables As seen in the examples above, shell variables are in uppercase characters by convention. Bash keeps a list of two types of variables: 3.2.1.1. Global variables Global variables or environment variables are available in all shells. Below is a typical output: Bash Reference Manual: Table of Contents.