Reputation: 399
I need to do some linux command operations on files having "!" like characters in filename. But whenever I am trying to execute the commands, I am getting below error.
[root@ATD-6000 ~]# cat a!aapoorv.txt
-bash: !aapoorv.txt: event not found
I am executing these commands in python using paramiko module. I can't use raw string r'filestringname', as I am reading string name from db itself.
How to escape/handle these king of characters using python/bash.
Upvotes: 2
Views: 581
Reputation: 22282
try this:
cat a\!aapoorv.txt
or this
cat 'a!aapoorv.txt'
Note that while
cat a\!aapoorv.txt
works in all shells that implement that csh-style history expansion,cat 'a!aapoorv.txt'
doesn't work in csh/tcsh.
for more information, you can see man bash
about the QUOTING.
Here is some of that document:
Quoting is used to remove the special meaning of certain characters or words to the shell.
Quoting can be used to disable spe‐cial treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expan‐sion.
And here is the output:
[kevin@Arch test]$ ls
a!aapoorv.txt
[kevin@Arch test]$ cat a\!aapoorv.txt
Hello, This is a test
[kevin@Arch test]$ cat 'a!aapoorv.txt'
Hello, This is a test
On Python, you don't need to escape the special character, here is a test:
>>> with open('a!aapoorv.txt') as f:
... f.read()
...
...
'Hello, This is a test\n'
>>> with open(r'a!aapoorv.txt') as f:
... f.read()
...
...
'Hello, This is a test\n'
>>>
Upvotes: 2
Reputation: 27704
For Bash, you need to use the escaping methods (single quotes or backslash) described by the other answers.
In Python, you shouldn't need to use raw strings or any other type of escaping to open a file with special chars.
For example, this works fine:
my_f_contents = open("a!aapoorv.txt").read()
Upvotes: 0
Reputation: 973
use singlequotes ' ':
$ cat 'a!aapoorv.txt'
cat: a!aapoorv.txt: No such file or directory
Upvotes: 0