> For the complete documentation index, see [llms.txt](https://documentation.dnanexus.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://documentation.dnanexus.com/getting-started/developer-tutorials/bash/bash-helpers.md).

# Bash Helpers

## Source Code

[View full source code on GitHub](https://github.com/dnanexus/dnanexus-example-applets/tree/master/Tutorials/bash/samtools_count_bashhelper_sh)

### Step 1. Download BAM Files

Download input files using the `dx-download-all-inputs` command. The `dx-download-all-inputs` command goes through all inputs and downloads them into folders with the pattern `/home/dnanexus/in/[VARIABLE]/[file or subfolder with files]`.

```shell
dx-download-all-inputs
```

### Step 2. Create an Output Directory

Create an output directory in preparation for the `dx-upload-all-outputs` DNAnexus command in the [Upload Results](#step-4-upload-result) section.

```shell
mkdir -p out/counts_txt
```

### Step 3. Run SAMtools View

After executing the `dx-download-all-inputs` command, there are three helper variables created to aid in scripting. For this applet, the input variable name `mappings_bam` with the platform filename `my_mappings.bam` has helper variables:

```shell
# [VARIABLE]_path is the absolute string path to the file.
$ echo $mappings_bam_path
/home/dnanexus/in/mappings_bam/my_mappings.bam
# [VARIABLE]_prefix is the file name minus the longest matching pattern in the dxapp.json file
$ echo $mappings_bam_prefix
my_mappings
# [VARIABLE]_name is the file name from the platform
$ echo $mappings_bam_name
my_mappings.bam
```

Use the bash helper variable `mappings_bam_path` to reference the location of a file after it has been downloaded using `dx-download-all-inputs`.

```shell
samtools view -c "${mappings_bam_path}" \
  > out/counts_txt/"${mappings_bam_prefix}.txt"
```

### Step 4. Upload Result

Use the `dx-upload-all-outputs` command to upload data to the platform and specify it as the job's output. The `dx-upload-all-outputs` command expects to find file paths matching the pattern `/home/dnanexus/out/[VARIABLE]/*`. It uploads matching files and then associates them as the output corresponding to `[VARIABLE]`. In this case, the output is called `counts_txt`. After creating the folders, place the outputs there.

```shell
dx-upload-all-outputs
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://documentation.dnanexus.com/getting-started/developer-tutorials/bash/bash-helpers.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
