2

I am using a program that takes a "command" as an argument and then adds some helpful wraping around it. Presumably in an effort to be helpful it appears to checks to make sure the command "exists". As far as I can tell this check is something like which. In my bash shell commands like set and export are built in and therefore which doesn't return anything.

Is there a way to set an environment variable without using a shell builtin.

My thought was to do something like define set.sh to be

#! /bin/bash
export $1

but this seems mildly crazy

StrongBad
  • 5,261

2 Answers2

3

No. Only the shell can change its own environment; set and export must always be built-ins.

This is also true of cd, alias and so on.

The best I can suggest is to reimplement your wrapper using a shell function. Either that or a shell script that you "source". Either way, a wrapper that runs inside the shell can do these things more seemlessly.

ams
  • 5,807
  • 1
  • 20
  • 27
1

A possible workaround for your original problem would be a wrapper for your wrapper (Xzibit image not included):

#!/bin/bash

command="$1"
temp=$(mktemp -d)

> "$temp/$command"
chmod a+x "$temp/$command"

PATH+=:$temp

your_wrapper

rm -r "$temp"

This creates an empty executable which will pass your wrapper's tests without overriding the command that you actually want to run (the builtin).

Chris Down
  • 125,559
  • 25
  • 270
  • 266