title: Copying files in ADB shell with run-as
body: <p>Is there a way to write a script that will copy files from an ADB shell using run-as?</p>
  
  <p>The only way I know of to copy in the adb shell is using <code>cat source &gt; dest</code> (edit: modern android versions have the <code>cp</code> command, which makes this question unnecessary), but I am only able to quote the greater-than sign one level deep - so my script can pass it to adb shell, but not to adb shell run-as.</p>
  
  <p>For example, this works:</p>
  
  <p><code>adb shell "cat source &gt; dest"</code></p>
  
  <p>But this does not:</p>
  
  <p><code>adb shell run-as "cat source &gt; dest"</code></p>
  
  <p>Nor this: </p>
  
  <p><code>adb shell "run-as cat source \&gt; dest"</code></p>
  
  <p>I even tried created a small script and uploading it to the device, but I can't seem to run the script from the adb shell - it tells me "permission denied". I can't chmod the script, either.</p>
  
  <p>The reason I want to do this is to copy a file into an app's private storage area - specifically, I am using a script to modify shared preferences and put the modified preferences back. Only the app itself or root can write to the file I want, however.</p>
  
  <p>The use case in this scenario is coping a file to a protected location on the device, not retrieving it; for retrieving, there are already good answers in <a href="https://stackoverflow.com/questions/18471780/android-adb-retrieve-database-using-run-as">this question</a>.</p>