Pages

Friday, May 14, 2021

typeset and declare

In bash, typeset and declare are exactly the same. The only difference is that typeset is considered obsolete.


typeset: typeset [-aAfFgilrtux] [-p] name[=value] ...

    Set variable values and attributes.

    Obsolete.  See `help declare'.

The man page even lists them in the same breath:


'typeset'

typeset [-afFrxi] [-p] [NAME[=VALUE] ...]

The 'typeset' command is supplied for compatibility with the Korn shell; however, it has been deprecated in favor of the 'declare' builtin command.


declare [-aAfFgilrtux] [-p] [name[=value] ...]

typeset [-aAfFgilrtux] [-p] [name[=value] ...]

    Declare variables and/or give them attributes.

typeset is portable to some other shells, for example, ksh93. If you are aiming for cross-shell portability, use typeset (and make sure that the way you are calling it is portable). If you don't care about such portability, use declare.

Difference between typeset and declare:

The typeset is more portable(e.g. ksh), while the declare is more preferable when portability is not a concern.

Difference between declare(or typeset) and local when used inside a function:

The typeset implies the declare, but more powerful. For example, declare -i x makes x have the integer attribute, declare -r x makes x readonly, etc.

local and declare are mostly identical and take all the same arguments with two exceptions: local will fail if not used within a function, and local with no args filters output to print only locals, declare doesn't.



ssh user@host "$(declare -f foo);foo"


#!/bin/bash

# Define your function

myfn () {  ls -l; }

To use the function on the remote hosts:


typeset -f myfn | ssh user@host "$(cat); myfn"

typeset -f myfn | ssh user@host2 "$(cat); myfn"

Better yet, why bother with pipe:


ssh user@host "$(typeset -f myfn); myfn"

Or you can use a HEREDOC:


ssh user@host << EOF

    $(typeset -f myfn)

    myfn

EOF

If you want to send all the functions defined within the script, not just myfn, just use typeset -f like so:


ssh user@host "$(typeset -f); myfn"

Explanation

typeset -f myfn will display the definition of myfn.

cat will receive the definition of the function as a text and $() will execute it in the current shell which will become a defined function in the remote shell. Finally the function can be executed.

The last code will put the definition of the functions inline before ssh execution.




No comments:

Post a Comment