Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

304
Views
How to handle single quote/ apostrophe and space together in linux cat command?

I have a file named test's file.txt inside test's dir directory. So the file-path becomes test's dir/test's file.txt. I want to cat the content of the file but since the file contains an apostrophe and a space it is giving me hard time achieving that. I have tried several commands including

  1. sh -c "cat 'test's dir/test's file.txt'"
  2. sh -c 'cat "test's dir/test's file.txt"'
  3. sh -c "cat '"'"'test's dir/test's file.txt'"'"'"
  4. sh -c 'cat "test\'s\ dir/test\'s\ file.txt"' and many more ... But none of them is working.

Some help would be really appreciated. Thank you!

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can use here-doc:

sh -s <<-'EOF'
cat "test's dir/test's file.txt"
EOF
over 4 years ago · Santiago Trujillo Report

0

Would you please try:

sh -c "cat 'test'\''s dir/test'\''s file.txt'"

As for the pathname part, it is a concatenation of:

'test'
\'
's dir/test'
\'
's file.txt'

[Edit]
If you want to execute the shell command in python, would you please try:

#!/usr/bin/python3

import subprocess

path="test's dir/test's file.txt"
subprocess.run(['cat', path])

or immediately:

subprocess.run(['cat', "test's dir/test's file.txt"])

As the subprocess.run() function takes the command as a list, not a single string (possible with shell=True option), we do not have to worry about the extra quoting around the command.

Please note subprocess.run() is supported by Python 3.5 or newer.

over 4 years ago · Santiago Trujillo Report

0

This option avoids the need for two levels of quoting:

sh -c 'cat -- "$0"' "test's dir/test's file.txt"

See How to use positional parameters with "bash -c" command?. (It applies to sh -c too.)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!