简体   繁体   中英

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

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM