Your Own Linux

Your Own Linux

The Linux command line can be a powerful tool for quickly running commands, parsing data and making changes to the system. This post will cover some of the basics which will get you up and running with bash. We’ll cover some of the bitshift operators people often use in bash, bitwise operators we come across in bash and arithmetic operations we do with numbers in bash. The most common way of getting around the linux command line is with the bash shell. Bash is a command line interpreter for running commands on the system. Although its part of the broader POSIX standard, most other shells are compatible with bash. The OS will automatically try to run bash if it can’t find another shell during startup.

The Shift Operators

Most people who’ve gone through a programming class will remember < , > & – for shifting values left, right and negative. That’s exactly what bash does with them. In addition to the above shift operators, bash supports additional shift operators. These are often used to manipulate strings that contain special characters.

Unshifted Positional Arguments

Number 0

Argument is bit field of 1s and is intended for use with unary operators (see below). Arithmetic multiplication, division and modulo (remainder) all work as expected on this argument.

Left Shifted Positional Arguments

Numbers 0-65535

Argument can be used as an index into an array: ${arrayname [$1]} where arrayname is the array. The $1 variable is set to the value of the one’s position in the array, i.e. [0] and [-32768] are equivalent. Arithmetic multiplication, division and modulo (remainder) all work as expected on this argument.

Right Shifted Positional Arguments

Numbers 0-65535

Argument can be used as an index into an array: ${arrayname [$1]} where arrayname is the array. The $1 variable is set to the value of the one’s position in the array, i.e. [0] and [-32768] are equivalent. Arithmetic multiplication, division and modulo (remainder) all work as expected on this argument. Bitwise Operators

Bash provides some additional operators which work on bits and bytes. You will most likely want to use these when you want to manipulate or parse data on a smaller level. There are two sets of bitwise operators: regular bitwise operators and and or xor unary operators.

A regular bitwise operator is an arithmetic operator that operates on the bits of a number, for example, the & operator checks if a number is even, which can be encoded as 4 bits in binary. Similarly, the | operator checks if there are left-to-right bitwise complement of a number.

The & and | operators take two arguments, X and Y . To get the bitwise AND (bitwise AND) value of X from Y , first use the & operator to look at the bits in X . Then assign a 1 to each bit that is on and set 0 for all other bits. This will produce a value of 1 for each row where the bits in X are all ones, which is what we want. Finally, use the | operator to check if each column of the output has all ones. If any column has more than one 1, then set all bits that are on to 0, otherwise leave them unchanged.

Conclusion

The bitwise operators are useful when you need to work with smaller chunks of data. As with most things in life, practice will make perfect. You will find yourself using these operators more and more as you get accustomed to bash.


Leave a Reply

Your email address will not be published.