HomeSoftwareGamesMusicForum

Editing variables

In contrast to zsh, neither bash nor ksh provide an internal command that allows editing shell variables interactively. To change a variable setting, a complete assignment name=value has to be typed in, which is inconvenient and error prone, especially with long values. The function vared offers a more convenient way. It creates a temporary file containing the actual variable assignments and launches an editor. The user can change the assignments at his will, as long as the shell syntax remains correct. After editing, the file is executed in the present environment, and then deleted. Using an editor allows editing any number of variables, including array elements, complete arrays, and even functions. Of course, any changes are lost at the end of the current shell. To keep the values, the assignments have to be put in a permanent (e.g., profile) file.

The function uses the editor given by the variable $EDITOR; default is vi.

Besides shell variable settings the function may also be used for editing function definitions. This is convenient if you want to create a function on the fly for testing purposes, and saves you from creating a file yourself, executing it, and deleting it afterwards. You can also modify an existing function without touching the original source, or edit an existing source by providing only the function name. In any case, the altered function is in effect right after closing the editor. See the next page for details.

As the two shells differ substantially here, separate versions for bash and ksh are provided.

Usage:

vared -h
vared {varname|[-a]arrayname|arrayname[index]|{-f|-F|-p} funcname}...
vared -h prints a short help.

Arrays: Option -a applies to indexed arrays only. If given, the following array is output in an index-based form, e.g.:

typeset -a array=([0]='x' [1]='y' [2]='z')
Without this option the output depends on whether or not the sequence of defined indices has gaps. If all indices are present, the sequential form of array assignment is used, e.g.:
typeset -a array=( 'x' 'y' 'z' )
If there is at least one gap, the index-based form is used. Associative arrays in ksh are always index-based.
up
Created 2011-08-11 by mopcoge