Linux string command line

Linux string command line

Only print strings from initialized, loaded data sections in the file. This may reduce the amount ofgarbage in the output, but it also exposes the strings program to any security flaws that may be presentin the BFD library used to scan and load sections. Strings can be configured so that this option is thedefault behaviour. In such cases the -a option can be used to avoid using the BFD library and insteadjust print all of the strings found in the file.

Print the offset within the file before each string. The single character argument specifies the radix ofthe offset—o for octal, x for hexadecimal, or d for decimal.

Select the character encoding of the strings that are to be found. Possible values for encoding are: s =single-7-bit-byte characters (ASCII, ISO 8859, etc., default), S = single-8-bit-byte characters, b =16-bit bigendian, l = 16-bit littleendian, B = 32-bit bigendian, L = 32-bit littleendian. Useful forfinding wide character strings. (l and b apply to, for example, Unicode UTF-16/UCS-2 encodings).

By default tab and space characters are included in the strings that are displayed, but other whitespacecharacters, such a newlines and carriage returns, are not. The -w option changes this so that allwhitespace characters are considered to be part of a string.

By default, output strings are delimited by a new-line. This option allows you to supply any string to beused as the output record separator. Useful with —include-all-whitespace where strings may contain new-lines internally.@fileRead command-line options from file. The options read are inserted in place of the original @file option.If file does not exist, or cannot be read, then the option will be treated literally, and not removed.Options in file are separated by whitespace. A whitespace character may be included in an option bysurrounding the entire option in either single or double quotes. Any character (including a backslash)may be included by prefixing the character to be included with a backslash. The file may itself containadditional @file options; any such options will be processed recursively.

Источник

Bash Scripting – String Manipulation

String manipulation is one of the fundamental concepts in bash scripting. In programming, strings are one of the data types which are an ordered sequence of characters. It is important that you know how to create and manipulate strings in bash. In this guide, we will learn string manipulation in Bash shell scripting with simple examples. You will be comfortable working with bash strings at the end of this article.

Читайте также:  Create an executable file in linux

Variable assignment

Strings can be assigned to a variable and later used in the script for further processing. For example, I am creating a variable named «GREET_USER» and printing the string to the terminal.

$ GREET_USER="Hello, Thanks for visiting OSTechnix"
$ echo "$GREET_USER"

Assigning string to variable

Bash has no strong type system so if you assign a value to a variable it will be treated as a string type. You can create strings with single, double, or no quotes.

There is a difference between single quotes and double quotes in bash. Single quotes prevent variable and command expansion while double quotes support it. Take a look at the below example.

I have created another variable named «SITE_NAME» and it is used in the «GREET_USER» variable. In double quotes, the variable will be expanded and in single quotes, the variable will not be expanded.

## DOUBLE QUOTES $ GREET_USER="Hello, Thanks for visiting $" $ echo "$GREET_USER"

Usage of double quotes

# SINGLE QUOTES

$ GREET_USER='Hello, Thanks for visiting $'
$ echo "$GREET_USER"

Usage of single quotes

Length of the string

To find the length of the string you can use the # symbol. Finding the length of the string will be useful in some cases where you have to write some logic based on the number of strings.

Length of the string

Converting strings to Array

There are many ways to convert string data type to array type. The most simple way would be to enclose the string inside curly brackets.

$ ARR_TYPE=($GREET_USER) $ echo $ $ for element in $; do echo $element done

String to Array

The second method would be to split the string and store it as an array based on the delimiter used in the string. In the previous example, space is used as the field separator (IFS) which is the default IFS in bash. For example, if you have a comma-separated string you can set the IFS to a comma and create an array. For more details about IFS, refer the following guide:

$ STR_TO_ARR="column1,column2,column3" $ IFS="," $ ARR=($) $ for element in $; do echo $element; done $ echo "$"

Custom IFS - Array conversion

Case conversion

Bash has built-in support for string case conversion. You have to use some special characters at the end of the string for case conversion like as shown below.

# SPECIAL CHARACTERS ,, ==> Converts an entire string to lowercase ^^ ==> Converts an entire string to Uppercase ~~ ==> Transpose Case , ==> Converts first letter alone to lowercase ^ ==> Converts first letter alone to uppercase

# —- LOWER TO UPPER CASE —- $ L_TO_U=»welcome to ostechnix» $ echo $ # —- UPPER TO LOWER CASE —- $ U_TO_L=»WELCOME TO OSTECHNIX» $ echo $ # —- TRANSPOSE CASE —- $ TRS_CASE=»Welcome To OsTechnix» $ echo $ # —- FIRST LETTER TO LOWERCASE —- $ F_TO_L=»OSTECHNIX» $ echo $ # —- FIRST LETTER TO UPPERCASE —- $ F_TO_U=»ostechnix» $ echo $

Читайте также:  Поддерживаемое программное обеспечение linux

Case conversion

You can also use regex pattern matching and convert the case for the matches.

$ L_TO_U=»welcome to ostechnix»
$ echo $

Regex pattern matching - Case conversion

String concatenation

You can concatenate multiple strings by placing the strings one after another. Depending upon how your strings are concatenated, you can add extra characters too.

$ SITE_NAME="OSTechnix"
$ MESSAGE="Welcome to"
$ echo "$ "

String concatenation

String slicing

String slicing is a way of extracting a part of a string using the index position. Each character in the string is assigned an Integer value with which can be used to grab a portion of the string. Index value ranges from 0 to N. Below is the syntax for slicing.

 START => Starting Index Position LENGTH => Length of the String from position START

If LENGTH is not specified then the string will be printed till the end from the index position START .

Slicing - Start to End

With LENGTH given, it will print the substring from the START index position and how many characters are to be printed.

Slicing - Start and Length

You can also reverse the string in many ways. The simplest way is to use the rev command. If you wish to do this in a bash way without using any external command then you have to write the logic manually.

Reverse string

Search and replace

There is a native way to search and replace characters in a string without using any external command like sed or awk .

To replace the first occurrence of substring, use the following syntax.


The first occurrence of X will be replaced by Y.

Take a look at the below example where the first occurrence of the word «linux» is replaced with LINUX in uppercase.

Читайте также:  Mail client для linux

Replace first occurrence

To replace all the occurrences of the word, use the following syntax.

Replace all occurrences

Remove substring

There are different ways to remove substring from the string. External utilities like sed , awk , or tr can be used or there is a way to do it in bash native way.

In the bash native way, parameter expansion is used to remove the substring. You have to use the % symbol followed by a pattern to remove. This will match the last found pattern and remove it.

For example, I wish to remove the words that come after the period ( . ) following syntax should be used. Here whatever comes after the period ( . ) will be removed. In this case, the last matched pattern .com is removed.

$ SITE_NAME=»www.ostechnix.com»
$ echo $

Remove substring using % symbol

To match the first occurrence of the pattern, use double percentage symbol

$ SITE_NAME=»www.ostechnix.com»
$ echo $

Remove substring using %% symbol

You can also use the # or ## symbol which will do a kind of inverse delete. With a single # symbol, the first pattern is matched and everything before the pattern is deleted.

$ SITE_NAME=»www.ostechnix.com»
$ echo $

Remove substring using # symbol

With the double ## symbol, the last pattern is matched and everything before the pattern is deleted.

$ SITE_NAME=»www.ostechnix.com»
$ echo $

Remove substring using ## symbol

Conclusion

In this article, we have seen how to create strings in bash and different ways to manipulate the strings. To get familiar with bash string manipulation, launch the terminal and start practicing the examples provided in this guide. If you have any feedback or question, please use the comment section below.

Bash scripting guides:

  • Bash Scripting – Printf Command Explained With Examples
  • Bash Scripting – Indexed Array Explained With Examples
  • Bash Scripting – Associative Array Explained With Examples
  • Bash Scripting – For Loop Explained With Examples
  • Bash Redirection Explained With Examples
  • Bash Scripting – Variables Explained With Examples
  • Bash Scripting – Functions Explained With Examples
  • Bash Echo Command Explained With Examples In Linux
  • Bash Heredoc Tutorial For Beginners
Karthick

Karthick is a passionate software engineer who loves to explore new technologies. He is a public speaker and loves writing about technology especially about Linux and opensource.

Источник

Оцените статью
Adblock
detector