Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

305
Visualizações
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 Respostas
Responde à pergunta

0

You can use here-doc:

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

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda