You are not logged in.
hi i have a script to install arch
https://git.system72.dev/dotfiles/tree/ … rchinstall
im experiencing a problem with heredocs, it doesnt let me assign variables inside of them, i was wondering if there was any alternatives or a solution to this prolbem. thanks.
p.s. im a shell scripting noob, also i dont know bash so the solutions please be posix.. thanks.
Last edited by system72 (Yesterday 18:53:57)
secure boot | luks | auto mounting | uki
Offline
im experiencing a problem with heredocs, it doesnt let me assign variables inside of them
What do you mean by "assign variables inside of heredoc"? Do you want to prevent variable expansion in generated content? You can escape dollar sign with backslash to do this, e.g.:
if [ "\${f}" != "/home/${username}/dotfiles/etc/default" ]; then
cp -r "\${f}" /etc
fiOffline
no i mean like $f is not assigned to anything when i do it in the heredoc
its just empty
it tells me this

Last edited by system72 (Yesterday 15:24:46)
secure boot | luks | auto mounting | uki
Offline
no i mean like $f is not assigned to anything when i do it in the heredoc
its just empty
Because "${f}" is expanded by shell before arch-chroot run. There is no such variable assigned in your script, it expands to empty string.
If you want heredoc to contain "${f}" literally, you have to escape the dollar sign as shown above.
Offline
so if i escape it then it will work
are there any alternatives to heredocs that are better for this usecase
Last edited by system72 (Yesterday 16:04:03)
secure boot | luks | auto mounting | uki
Offline
Just quote the EOF:
command <<"EOF"…Offline
ok thank you that seems better, are there any implicaitions of doing it that way that will mess with some other operations inside of the heredoc?
secure boot | luks | auto mounting | uki
Offline
Quoting the delimiter will prevent any shell expansion within the heredoc (variables, command substitution, etc.). If you want some things to be expanded and others to be literal, you'll have to escape individually. You can redirect it to a file instead of running it directly to check the resulting script for anything unexpected.
Offline
im confused on what you mean can you give me some examples from my existing script. thanks.
secure boot | luks | auto mounting | uki
Offline
Try something like that:
var=123
cat <<EOF
var = $var
date = $( date )
math = $(( var * 2 ))
EOFUnquoted, so stuff will be expanded. Run it with a quoted EOF for comparison. And if you want the literal date and the rest expanded, you need to manually escape as required:
var=123
cat <<EOF
var = \$var
date = $( date )
math = \$(( var * 2 ))
EOFOffline
thanks, as i understand it, if the EOF is quoted then all variables outside of the heredoc cannot be accessed inside of the heredoc but all variables inside of the heredoc get expanded correctly
so i should just escape manually right?
hm actually, it seems if the EOF is quoted you need to export the vars outside of the heredoc for them to be accessable. ok the quoting seems good for now. please correct me if im wrong
Last edited by system72 (Yesterday 18:43:06)
secure boot | luks | auto mounting | uki
Offline
Right. Think of it as a separate shell script (in the case of arch-chroot anyway, it just runs Bash if you don't give it a command to run, so you're basically feeding the heredoc to the bash command). And exported variables will be available in the script.
Offline
thank you for your time
secure boot | luks | auto mounting | uki
Offline