Linux Read Whitespaces and Special Characters

how to read files with spaces and special characters in their name?

escapeshellarg

Incidentally, you should always use this for any shell arguments, less you want to make your application open to shell injection-attacks, which could be very dangerous.

How to know difference between [ENTER] and [SPACE] in bash

Try this Shellcheck-clean code:

#! /bin/bash

read -r -N1 Input

case $Input in
$'\n') echo "Enter is Pressed";;
' ') echo "Space is pressed";;
esac
  • The -r option for read prevents special handling of backslash (\) characters.
  • The -N (instead of -n) option for read prevents special handling of delimiter characters (including space and newline).
  • $'\n' is Bash ANSI-C Quoting.
  • I used case instead of if just because it's a bit less verbose in this example.

How to distinguish whitespace in string after using read in bash shell

You can try this.
Instead of trying that there are a lot of things you can learn when using vim/vi/vim.exe/vi.exe in any operating system (Windows cygwin/mingw, AIX, Linux HP-UX/OSF1/IRIX/SunOS/UNIX)
Hence try learning vi.

However I have updated your code only for my knowledge transfer:
Here goes my coding:

#!/bin/bash
echo "Use any one of the following keys:"
UP=$(echo $'\U2190')
#echo -n $'\U2190' | od -bc
#echo -e "\0342\0206\0220" # LEFT
LEFT=$(echo $'\U2191')
#echo -e "\0342\0206\0221" # UP
RIGHT=$(echo $'\U2192')
#echo -e "\0342\0206\0222" # RIGHT
DOWN=$(echo $'\U2193')
#echo -e "\0342\0206\0223" # DOWN
echo SPACEBAR
while read -rsN1 key
do
if [ $'\e' == "$key" ]
then
read -sN1 -t 0.0001 k1
read -sN1 -t 0.0001 k2
read -sN1 -t 0.0001 k3
key+=${k1}${k2}${k3}
fi
case $key in
$'[A')
echo -n "$UP"
;;
$'[B')
echo -n "$DOWN"
;;
$'[C')
echo -n "$RIGHT"
;;
$'[D')
echo -n "$LEFT"
;;
' ')
echo -n " "
;;
#It doesn't work here!!!
i)
echo "insert"
;;
esac
done

My output:

$ 73172247.sh
Use any one of the following keys:
SPACEBAR
↑↓←→↓↑ ↑↑ ↑ →←↑
Example pdf:
https://www.jics.utk.edu/files/images/csure-reu/PDF-DOC/VI-TUTORIAL.pdf
Textpad.exe/notepad++.exe at windows
gedit at Linux.

mv a file from textpad that have space and special character

This command reads each line from file.txt, then renames the files, substituting _ for each special character.

$ while read line ; do mv "$DIR/$line" "$DIR/${line//[ ()@$]/_}" ; done < file.txt 

How to add a space after special characters in bash script?

The following assumes you want to add a space after every character in the !@#$% set (even if it is the last character in a line). Test file:

$ cat file.txt
a!a
@bb
c#c
$dd
ee%
foo
%b%r
$ sep='!@#$%'

With sed:

$ sed 's/['"$sep"']/& /g' file.txt
a! a
@ bb
c# c
$ dd
ee%
foo
% b% r

With awk:

$ awk '{gsub(/['"$sep"']/,"& "); print}' file.txt
a! a
@ bb
c# c
$ dd
ee%
foo
% b% r

With plain bash (not recommended, it is too slow):

$ while IFS= read -r line; do
str=""
for (( i=0; i<${#line}; i++ )); do
char="${line:i:1}"
str="$str$char"
[[ "$char" =~ [$sep] ]] && str="$str "
done
printf '%s\n' "$str"
done < file.txt
a! a
@ bb
c# c
$ dd
ee%
foo
% b% r

Or (not sure which is the worst):

$ while IFS= read -r line; do
for (( i=0; i<${#sep}; i++ )); do
char="${sep:i:1}"
line="${line//$char/$char }"
done
printf '%s\n' "$line"
done < file.txt
a! a
@ bb
c# c
$ dd
ee%
foo
% b% r

How can I confirm whether whitespace or special characters are escaped in a wildcard pattern?

  1. Am I correct that wildcard expansion results in a set of strings that contain escaped special characters

No. There is no need for the shell to escape special characters at that point, because filename expansion is the last word expansion to be performed; strings resulting from it are not subjected to word splitting or any other expansion; they stay as-is. This is documented in the manual as follows:

The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution (done in a left-to-right fashion); word splitting; and filename expansion.



Related Topics



Leave a reply



Submit