Named parameters in Linux Bash scripts

There are basic positional parameters in bash scripts that might be the first way that one learns how to add parameters to bash scripts. However there should be a way to add named parameters to a script also, and to do so in a way in which it does not take to much time to do so. Often I want to write a bash script that preforms some kind of task other then that of parsing options.

Well in bash there is a built in command that might prove to be the first solution that comes to mind when it comes to having named parameters in a script. In this post I will be going over a few examples of that built in command, and also write about other topics that might come up in the process of doing so.

Read More

Special parameters in Linux Bash scripts

So I wrote a bash scripts post on parameters in bash, and with that positional parameters might be the first thing most people will think of when it comes to parameters for a script. However it is important to refer to positional parameters as positional parameters rather than just simply parameters, because there is more than one set of parameters at play when a script is called. Yes there are the positional parameters that are passed to a script, but there is also any and all parameters that might have been passed to the bash command itself, and in addition to this there are also a number of Special parameters in bash to work with also.

So then in other words in bash there are positional parameters such as $0, $1, $2, and so fort these parameters refer to the name of the command called, followed by each option that was passed to the command that is separated by a space. However there is also a number of parameter for the bash command itself that is called before the command is called. In addition two two sets of parameters for the bash script and bash itself there is also parameters such as $@ that is a way to quickly expand all of these positional parameters, and $# that will give a count of these. These two parameters are examples of special parameters and there is a number of them to cover in bash.

So if I aim to write an comprehensive collection of posts on the features of bash scripts it is called for to write one on these special parameters. In this post I will be going over all of these kinds of parameters with at least one basic example of each as I go over them.

Read More

Arrays in Linux Bash scripts

Arrays in bash scripts can be indexed or associative. There is a simple syntax that can be used to create indexed arrays with ease, and the declare bash built in command can be used to create associative arrays. These are the two kinds of arrays that are supported in bash, and there is no support of multidimensional arrays in bash.

There is not just the question of how to go about creating an array in bash, but there is also how to go about looping over them, and preform all kinds of various tasks that one might be familial with in other languages. Keep in mind that there is much that is lacking when it comes to things like class methods for example. However the basic functionality that one might expect in a language such as bash is very much there.

Read More

Parameter Expansion in Linux Bash scripts

One core feature of Bash that I have been using all the time when writing bash scripts thus far is Parameter Expansion. There are several forms of Parameter expansion but they all have to do with how to go about creating values for variables and strings to be used with commands. There is the basic braces expansion that is used as a way to separate a variable name from the rest of a string value, as well as preform something know as variable indirection more on that later.

There are also several other forms of parameter expansion, each of which might deserve there own post actually. However I often have a central post on topics such as this where I at least cover the basics of each, so this will be that kind of post when it comes to the various forms of parameter expansion in bash.

Read More

Arithmetic in Linux Bash scripts

When it comes to Arithmetic in bash scripts it would seem that doing some basic operations can be done, however when it comes to doing anything a little advanced it might be best to wrap another programing environment.

For the most part I would say that I do not need to do any advanced programing with bash scripts when it comes to working out some kind of complicated expression. The use case for bash scripts is that it just needs to work okay as a glue of sorts between applications, and to serve as a way to make it so I do not have to type a long string of commands in every time. So far I can not say that I need to do much beyond just simple addition and subtraction actually, and if I ever end up in a situation in which I need to do something a little advanced, it might be best to do that in another language such as javaScript, or python.

Still some basic Arithmetic is possible with bash alone, and in this post I will be going over some basic examples of how to preform some basic addition, and subtraction operations. I could get into some more complex expressions, but doing so often might require wrapping a more advanced programing environment and when doing that it might be best to just work out what I need to do as a program written in that environment.

Read More