title: bash script to change directory and execute command with arguments
body: <p>I am trying to do the following task:
  write a shell script called <code>changedir</code> which
  takes a directory name, a command name and (optionally) some additional arguments.
  The script will then change into the directory indicated, and
  executes the command indicated with the arguments provided.</p>
  
  <p>Here an example:</p>
  
  <pre><code>$ sh changedir /etc ls -al
  </code></pre>
  
  <p>This should change into the <code>/etc</code> directory and run the command <code>ls -al</code>.</p>
  
  <p>So far I have:</p>
  
  <pre><code>#!/bin/sh
  directory=$1; shift
  command=$1; shift
  args=$1; shift
  cd $directory
  $command
  </code></pre>
  
  <p>If I run the above like <code>sh changedir /etc ls</code> it changes and lists the directory. But if I add arguments to the <code>ls</code> it does not work. What do I need to do to correct it?</p>