Bash technique: assign a variable to a newline character (judge whether the file ends with a newline character)

Time:2021-1-27

Variable assignment to newline

In Bash, if you want to assign a variable as a newline character, it has no effect to write it as’ \ \ n ‘. You need to write it as $’ \ \ n ‘. Specific examples are as follows:


$ newline='\n'
$ echo $newline
\n
$ newline=$'\n'
$ echo $newline

You can see that assigning the newline variable to ‘n’ results in the string n, not the newline itself.

This is the difference between Bash and C.
In C language, ‘n’ corresponds to the newline character itself and has only one character, while ‘n’ corresponds to a string.
But in Bash, ‘n’ also corresponds to a string.

The newline itself can be obtained by assigning the newline value to $’\ n’. See man bash’s description of this writing method as follows:

Words of the form $’string’ are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:


 \n  new line
 \r  carriage return
 \t  horizontal tab
 \'  single quote

The expanded result is single-quoted, as if the dollar sign had not been present.

In other words, $’string’can use the escape character of C language to get the corresponding character itself.

Determines whether the last line of a file ends with a newline character

In Linux, you can use the following command to determine whether the last line of a file ends with a newline character:

test -n “$(tail filename -c 1)”

Here, use the command tail file name – C 1 to get the last character of the file name.

In actual use, you need to change the file name to the specific file name to be judged.

The tail command gets the content at the end of the file. Its – C option specifies how many bytes to get at the end of the file.

See man tail’s description of the – C option as follows:

-c, –bytes=K

output the last K bytes; alternatively, use -c +K to output bytes starting with the Kth of each file.

That is, the tail – C 1 command specifies to get the last character of the given file.

After getting the last character of the file, judge whether the character is a newline character or not. Here, we can’t judge whether the character is equal to the newline character directly, but judge whether the character is empty.

The reason is that bash removes the line break at the end when using the $(tail file name – C 1) command substitution to get the output of the internal command.

So when the last line of the file ends with a newline character, $(tail file name – C 1) command replacement will remove the newline character obtained, and the final result will be empty, and the newline character itself will not be returned.

See man bash’s description of command substitution as follows:

Command substitution allows the output of a command to replace the command name. There are two forms:


  $(command)
 or
  `command`

Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting.

As you can see, after command replacement, the line break at the end will be removed.

because$(tail filename -c 1)Command substitution removes the line break at the end, which is used heretest -nTo determine whether the final result is an empty string.

If the last line of the file ends with a newline, then$(tail filename -c 1) The result of is empty,test -nThe command returns 1, which is false.

If the last line of the file does not end with a newline, then$(tail filename -c 1) The result of is not empty,test -nThe command returns 0, which is true.

According to the actual needs, we can usetest -zTo judge. If the last line of the file ends with a newline,$(tail filename -c 1) The result of is empty,test -zThe command returns 0, which is true.

So far, this article about bash technique: assign a variable to a newline character, and judge whether the file ends with a newline character. For more related variable assignment to a newline character, please search previous articles of developer or continue to browse the following related articles. I hope you can support developer more in the future!

Recommended Today

Practice of query operation of database table (Experiment 3)

Following the previous two experiments, this experiment is to master the use of select statements for various query operations: single table query, multi table connection and query, nested query, set query, to consolidate the database query operation.Now follow Xiaobian to practice together!Based on the data table (student, course, SC, teacher, TC) created and inserted in […]