Bash: Including a path from one file into another

  • Thread starter jf22901
  • Start date
  • Tags
    File Path
In summary: I tried this and it didn't work either. I'll stick with the set command.In summary, the person is trying to figure out how to import a path specified in another file into their bash scripts. They initially tried using a file called path.sh, but it didn't work. They then tried using an environment variable, but that didn't work either. They then tried using a master script that knows the path, but that didn't work either. They then tried using "export", but that didn't work either.
  • #1
jf22901
55
1
Hi everyone

I'm having trouble working out how to import a specified path into a bash script. Basically, I have numerous scripts, each of which writes their output to a certain directory via the command

Code:
set wdir = "/home/..."

However, in the future this directory may change, so rather than having to manually change the path of the directories in each file, I was wanting to create one file that specifies the path, and then have the other files include it. That way I only have to change one file if the path changes.

Initially I thought of having the path specified in a file such as 'path.sh', which would simply look like

Code:
FILEPATH="/home/..."

and then I thought that in the other files that require the path I could just put

Code:
#!/bin/bash
.
.
.
source path.sh
set wdir = $(FILEPATH)
.
.
.

However, such coding does not work, and I have no idea how to go about fixing it. Could anyone offer any suggestions as to the best way to include a path specified in another file? I'm new to all this bash scripting stuff! :tongue2:

Many thanks
 
Technology news on Phys.org
  • #2
try: export FILEPATH="/home/..."

I think the simple set command is only effective for the actual script runtime. I'd have to fiddle with it to prove it, especially when source-ing the file though.
 
  • #3
Have you considered the option of making the path an argument?

How about using an environment variable?

How about a master script that knows the path, and you select which individual script you want to run as an argument to the master one?



If you are set on this approach, you can use "export" as schip mentioned. Alternatively, you could write a script that writes the filename to stdout and use something like

wdir = `path.sh`
 
  • #4
Thanks for the replies everyone :cool:

I tried putting 'export' into the file, but it still wouldn't work. However, if I leave everything as it is but change $(FILEPATH) to $FILEPATH, it works. I'll stick with this for now, but will look into the other methods suggested.

Thanks once again
 
  • #5
Use
FILEPATH=$0
Thats all you need.
 

1. How do I include a path from one file into another in Bash?

To include a path from one file into another in Bash, you can use the source command followed by the file path. For example, if you want to include the file "script.sh" into another file, you would use source script.sh.

2. Can I include a relative path in Bash?

Yes, you can include a relative path in Bash. The path will be relative to the current working directory. For example, if your current working directory is "home/user", and you want to include the file "script.sh" which is located in "home/user/scripts", you would use source scripts/script.sh.

3. What is the difference between using "source" and "dot" when including a path in Bash?

The source command and the . (dot) command are both used to include a path from one file into another in Bash. The main difference is that source will search for the file in the current working directory and the directories specified in the $PATH variable, while . will only search in the current working directory.

4. Can I use variables in the file path when including a file in Bash?

Yes, you can use variables in the file path when including a file in Bash. For example, if you have a variable filename="script.sh", you can use it in the source command like this: source $filename.

5. Is it possible to include multiple files in Bash?

Yes, it is possible to include multiple files in Bash. You can use the source command multiple times to include different files in the same script. Just make sure to include them in the correct order, as the code in the included files will be executed in the order they are included.

Similar threads

  • Programming and Computer Science
Replies
2
Views
603
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
9
Views
852
  • Programming and Computer Science
Replies
21
Views
461
  • Programming and Computer Science
Replies
21
Views
2K
Replies
19
Views
1K
Replies
9
Views
3K
  • Programming and Computer Science
Replies
12
Views
9K
Back
Top