I am passing a file name with path like /tmp/user/abc.txt to shell script,

there I want to read the abc.txt and get the content in a variable.

right now am using export SUBJECT=echo ${1} SUBJECT_1=cat $SUBJECT`

this is not printing anything . please help how to print, Additionally , the content of this file will have some Japanese characters also.


You have a few mistakes in your code:

  1. You have a missing backquote here SUBJECT_1=cat $SUBJECT`.
  2. export SUBJECT=echo ${1} is a false statement. It should be export SUBJECT="${1}".
  3. Backquotes execute the code inside them in a subshell, so it will not print your file's content. If you want to print it you will have to add this line: echo "${SUBJECT_1}".

Once you fix these the code will work, but here are a few tips you should consider:

  1. You should use $(code) if you want to execute code in a subshell instead of using backquotes, you can read about it in this SO thread.
  2. Next time when you're stuck on shell code try this first, it has great debugging capabilities for shell code and you can find the command line version of it.
  3. Always use double quotes on variables, it keeps the variable from splitting. You can read about it here.
| improve this answer | |
  • HI Samy, I tried your suggestion still printing nothing actually, – FedUpWithPowerHungryHumans Jul 29 '17 at 10:52
  • HI Samy, I tried your suggestion still printing nothing actually, Let me explain the issue more clearly in Shell Script the is a variable SUBJECT if do echo $SUBJECT I get /usr/tmp/abc.txt when I do echo $(cat /usr/tmp/abc.txt) in shell script , its not printing anything, but if I do cat /usr/tmp/abc.txt in the terminal , its printing the value. All I need to do is to get the value of abc.txt which is in SUBJECT ie usr/tmp/abc.txt in another variable – FedUpWithPowerHungryHumans Jul 29 '17 at 10:59
  • Why don't you edit your question and paste your entire script for clarity? Also, add two sections: Expected output and Actual output. – Samy Jul 29 '17 at 11:17
  • thanks.. that's resolved , issue was there was a space between = symbol and backtick :) – FedUpWithPowerHungryHumans Jul 29 '17 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.