Home > OS >  How to use a variable or name instead of path of the file in a command in linux?
How to use a variable or name instead of path of the file in a command in linux?

Time:09-03

I have a shell script sample.sh. Inside the shell script, there are many commands, it looks like this for example:

#!/bin/bash

command 1 ......

command 2 ......

command 3 ......

txt1="/users/doc/folder1/sam.txt"
txt2="/users/doc/folder2/pam.txt"
txt3="/users/doc/folder3/ram.txt"

echo "run done"

First I gave a run with this script like sh sample.sh. After running this shell script I want to run a command in which I wanted to give txt3 which was the name I'm using instead of the path for the file ram.txt

The command looks like this for eg:

convert -i txt3 > sim.tsv

This gave me an error. Error: The requested txt file (txt3) could not be opened. Exiting!

May I know how this works without giving the path to the file in the command?

CodePudding user response:

You need to source the script, not run it, and export your variables to have them persist after the script finished.

You have to refer to the variable as $txt3 after the sourcing.

  • Related