Identifying and removing null characters in UNIX
I’d use tr
:
tr < file-with-nulls -d '\000' > file-without-nulls
If you are wondering if input redirection in the middle of the command arguments works, it does. Most shells will recognize and deal with I/O redirection (<
, >
, …) anywhere in the command line, actually.
Assign string containing null-character (\0) to a variable in Bash
In Bash, you can't store the NULL-character in a variable.
You may, however, store a plain hex dump of the data (and later reverse this operation again) by using the xxd
command.
VAR1=`echo -ne "n\0m\0k" | xxd -p | tr -d '\n'`
echo -ne "$VAR1" | xxd -r -p | od -c # -> 0000000 n \0 m \0 k
How can I write a null ASCII character (nul) to a file with a Windows batch script?
Okay, this was tricky, and the solution is ugly, but it works.
You can use the batch file itself as the file containing the null character to be copied.
This batch file, called null.bat
:
findstr /v /r \n null.bat >> myfile.txt
[NULL]
(where the last line contains only the null character) appends the null character to myfile.txt
.
findstr /v /r
shows all lines that aren't matched by the regex, i.e. only the last one, because there's no newline character.
I have tried several other things, but this was the only one I could find that didn't strip the null character.
Note: findstr
was first shipped with Windows 2000 so may not be available on prior versions of Windows
User typing null terminator in scanf
On many systems, the answer is "Yes".
Usually, the magic sequence is Control-@.
The character code for @ is usually 64, one less the that of A (65). Control-A is character code 1; one less is character code 0, aka '\0'
.
Note that to get a zero-length input, you'd have to type the null byte as the first non-space character, and you would still need to hit return to enter the input. Your program would find it hard to reliably identify what else was entered after the null byte.
How to 'cut' on null?
Just specify an empty delimiter:
cut -d '' -f1
(N.B.: The space between the -d
and the ''
is important, so that the -d
and the empty string get passed as separate arguments; if you write -d''
, then that will get passed as just -d
, and then cut
will think you're trying to use -f1
as the delimiter, which it will complain about, with an error message that "the delimiter must be a single character".)
in linux, is it normal that there is no null character at the end of file
The filesystem records the number of bytes in a file, and all the bytes are free to have any value - no particular character/byte value is a reserved sentinel value meaning end-of-file. So, you can have a NUL anywhere in the file, but don't need one to mark the end.
Each line in a text file should indeed be terminated with a linefeed, ASCII 10 dec, 0A hex (on Windows it'd be a carriage return ASCII 13 dec followed by a linefeed). If you create an empty file ala echo > filename
it will have one linefeed, but only because echo prints an empty line by default. If you instead used touch filename
it would be completely empty.
When you cat > filename
and type things into your terminal/console window, you eventually use Control-D to trigger an end-of-file condition (for Linux / Control-Z in DOS), but that character is not stored in the file itself.
C - Printing null character using %c in nested printf statement
printf("%d", printf("%c", '\0'));
Why this line prints 1?
The value '\0'
is one single character, which "%c"
sends th stdout (though you can't see it).
printf("%d", printf("%s", "\0"));
Why this line prints 0?
The string "\0"
has two characters in it: the literal '\0'
and the implied '\0'
. None of them are printed with "%s"
, the first zero terminates the string. The string "foo"
has 4 characters: the 3 literal ones and the implied '\0'
Related Topics
What Length Can a Network Interface Name Have
Bash Not Trapping Interrupts During Rsync/Subshell Exec Statements
Grep Fails Inside Bash Script But Works on Command Line
Explanation of Memcpy Memmove Glibc_2.14/2.2.5
Linux Kernel Headers' Organization
Checking Up Intel Assembly Opcodes Easily in Linux
Sed Is Printing a Substituted Line Twice
Convert Multiple Sftp Command Lines to a Single Line Command Line
Linux How to Add a File to a Specific Folder Within a Zip File
How to Find List of Odbc Drivers Installed on Linux
How to Store Linux Command Output into a Variable in Puppet
What Is the Use of Gfp_User Flag in Kmalloc
Provide Password Using Shell Script
Is There an Scp Variant of Mv Command
How to Monitor Newly Created File in a Directory with Bash
Best Approach to Detecting a Move or Rename to a File in Linux