2

Tengo este código:

     Phpversion=$(find /opt -type d -name PhpStorm* | cut -d "/" -f 3)
        touch /home/$HOSTNAME/.local/share/applications/PhpStorm.desktop
cat > "/home/$HOSTNAME/.local/share/applications/PhpStorm.desktop" << 'EOF'

    [Desktop Entry]
                    Name=PhpStorm
                    Type=Application
                    Exec=/opt/\$Phpversion/bin/phpstorm.sh
                    Icon=/opt/\$Phpversion/bin/phpstorm.png
                    Comment=Integrated Development Environment
                    Categories=GNOME;GTK;Development;IDE;
                    StartupNotify=true
                    MimeType=text/plain;
    EOF

El resultado de esto es que el fichero Phpstorm.Desktop no guarda el resultado de la variable $Phpversion.

He probado con \ y sin ella y no consigo hacerlo funcionar, ¿Alguna solución?

2
  • ¿los espacios a principio de línea están exactamente tal y como lo indicas en la pregunta?
    – fedorqui
    el 12 nov. 2018 a las 11:16
  • efectivamente, pero eso funciona igualmente. el 12 nov. 2018 a las 11:22

1 respuesta 1

2

Para que EOF expanda las variables debes ponerlo sin comillas:

cat > "/home/$HOSTNAME/.local/share/applications/PhpStorm.desktop" << EOF
#                                                                     ^^^

en lugar de

cat > "/home/$HOSTNAME/.local/share/applications/PhpStorm.desktop" << 'EOF'
#                                                                     ^^^^^

Veamos un ejemplo:

$ var="hola"
$ cat << EOF          # aquí se expande
> var vale $var
> EOF
var vale hola
$ cat << 'EOF'        # aquí no se expande
> var vale $var
> EOF
var vale $var

Esto se explica en el manual de Bash, sección 3.6.6. Here documents:

The format of here-documents is:

[n]<<[-]word
        here-document
delimiter

No parameter and variable expansion, command substitution, arithmetic expansion, or filename expansion is performed on word. If any part of word is quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence \newline is ignored, and ‘\’ must be used to quote the characters ‘\’, ‘$’, and ‘`’.

Es decir: si la palabra que usas (EOF en tu caso) no está entrecomillada, todas las líneas del here-document se ven afectadas por la expansión de parámetros, sustitución de comandos y el resto de cosas habituales.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.