Unix
- 16 April
Remove Character from String in Bash
1. Overview In this article, we will explore different ways to remove characters in String in different scenarios such as removing specific characters, removing first character, removing last character, and removing first and last characters. 2. Using Parameter Expansion Bash’s built-in parameter expansion is best suited for performing string manipulation operations. Let’s explore parameter expansion […]
- 15 April
Remove Double Quotes from String in Bash
1. Overview Removing double quotes from strings is a s crucial task in many scenarios, such as parsing JSON data, cleaning up user input, or preparing strings for further processing. In this article, we will see different ways to remove double quotes from String using tr, parameter expansion, awk, sed and grep. 2. Introduction to […]
- 15 April
Replace Character in String in Bash
1. Overview In this article, we will explore different ways to replace characters in String in different scenarios such as replacing the first or all occurrences of a given character, replacing all occurrences of multiple characters, and substituting a complete word. 2. Using Parameter Expansion Bash’s built-in parameter expansion can be used to manipulate String […]
- 14 April
Bash Echo to stderr
Using >&2 Operator Use the >&2 operator to redirect the echo output to stderr in bash. [crayon-67684a7aa9fb4925448750/] [crayon-67684a7aa9fb8497324586/] As shown above, an error message is an output to the stderr stream using the echo command with the >&2 operator. Here, stderr stands for standard error. It is one of the three standard streams. In Bash, […]
- 14 April
Call Python Script from Bash with Arguments
Python is a high-level language famous for its simplicity, flexibility, and readability. At the same time, Bash is a Unix shell and command language used primarily on Unix and Linux systems. Data analysis, machine learning, web development, and scientific computing tasks are widely performed using python language. On the contrary, the bash is used for […]
- 05 April
Convert Float to Integer in Bash
Using printf Command Use the printf command to convert float to integer in bash. [crayon-67684a7aaa204638670421/] [crayon-67684a7aaa207315500553/] First, the float_num variable is initialized with the floating-point number. Then to convert the floating-point number to an integer, the printf command is used with the format string %.0f (specifies that printf should output the floating-point number with zero […]
- 04 April
Run String as Command in Bash
Using eval Command Use the eval command to run a string as a command in Bash [crayon-67684a7aaa307851377543/] [crayon-67684a7aaa30a337273095/] In the above code, the eval command evaluates and executes the arguments passed to it as a Bash command. The argument passed to eval is an echo Hello, world! string. This string represents the command that we […]
- 29 March
Get Script Directory in Bash
Using BASH_SOURCE Array To get the current script’s directory in a Bash, use the BASH_SOURCE array with pwd command. [crayon-67684a7aaa3f3557095507/] [crayon-67684a7aaa3f5746844760/] [crayon-67684a7aaa3f6909744204/] In the above example, the BASH_SOURCE variable is used with dirname to get the path of executing the script directory. In Bash, the BASH_SOURCE is an array variable containing the list of source […]
- 27 March
Generate Random String in Bash
Using $RANDOM Variable Use the $RANDOM variable with cut command to generate random string in Bash. [crayon-67684a7aaa5b8963164340/] [crayon-67684a7aaa5bb578188211/] Bash provides a built-in variable $RANDOM that generates random numbers between 0 and 32767. We can use this variable to generate random strings of any length. Here’s how to generate a random string of length 10. The […]