Reputation: 127
I'd like to execute a range of lines from a shell script from a running shell and using the current shell's environment.
For example, in a script that sets a variable to something I don't want to type (say an API key or MAC address):
line
1 #!/bin/zsh
2
3 # Set the MAC address:
4 MAC="12-34-56-78-90-ab-cd"
...
...and in my shell, I'd like to grab line 4 above, run it, and have $MAC exist in that environment
I've tried sed -n '4p' script.zsh | zsh
but that doesn't affect the current shell I'm working in:
$ MAC="this is not a MAC address"
$ sed -n '4p' script.zsh | zsh
$ echo $MAC
---
this is not a MAC address
I could just copy and paste, but I'd like a solution I can use without touching my mouse - or when I don't have a mouse available.
Upvotes: 0
Views: 252
Reputation: 14490
You could combine your sed
command with a process substitution and source it:
source <(sed -n '4p' script.zsh)
though you might want to use a pattern match for the print line in case the line numbers shift.
source <(sed -n '/^MAC=/p' script.zsh)
Upvotes: 2